当前位置:网站首页>Simplified path (force buckle 71)

Simplified path (force buckle 71)

2022-04-23 18:48:00 Xiao Qin doesn't lose his hair

Deque,Queue,stack Simple understanding

Deque Implement a two terminal queue ,Queue fifo

Queue Deque
Add elements to the end of the team add()/offer() addLast()/offerLast()
Take the first element of the team and delete remove()/poll() removeFirst()/pollFirst()
Take the first element of the team but do not delete element()/peek() getFirst()/peekFirst()

 stack( Stack , The principle is First in, then out , Last in, first out ) Common methods :

①push() Push

②top() Get the stack top element

③pop() Pop up top element

④empty() It can detect stack If the inside is empty , return true It's empty , return false Is not empty

⑥size() return stack The number of internal elements

Simplified path

Give you a string path , Indicates a point to a file or directory  Unix style Absolute path ( With '/' start ), Please translate it into a more concise specification path .

stay Unix Style file system , One point (.) Represents the current directory itself ; Besides , Two points (..)  Means to switch the directory to the next level ( Point to the parent directory ); Both can be part of a complex relative path . Any number of consecutive slashes ( namely ,'//') Are treated as a single slash '/' . For this problem , Points in any other format ( for example ,'...') Are considered documents / Directory name .

Please note that , Back to Canonical path The following format must be followed :

Always with slash '/' start .
There must be only one slash between two directory names '/' .
Last directory name ( If there is ) You can't With '/' ending .
Besides , The path contains only directories on the path from the root directory to the target file or directory ( namely , Not included '.' or '..').
Return the simplified Canonical path .

 

 

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String path = sc.next();
		simplifyPath(path);
	}

	public static String simplifyPath(String path) {
		String[] str = path.split("/");//  according to "/" Split paths into arrays 
		Deque<String> deque = new ArrayDeque<String>();
		for (String string : str) {
			if (string.equals("..")) {// ".." Switch to the previous Directory , Therefore, you need to delete a layer of path 
				if (!string.isEmpty()) {
					deque.pollLast(); //  Here is to delete a layer of path 
				}
			} else if (string.length() > 0 && !".".equals(string)) {//  That is, the current element length of the array is greater than 0, And not for "."
				deque.offerLast(string);//  Add the current element to ArrayDeque A party 
			}
		}
		StringBuffer sbf = new StringBuffer();
		if (deque.isEmpty()) {
			sbf.append("/");
		} else {
			while (!deque.isEmpty()) {
				sbf.append("/");
				sbf.append(deque.pollFirst());
			}
		}
		System.out.println(sbf.toString());
		return sbf.toString();
	}
}

Shallow deque.isEmpty() Changed to deque==null, If you change it all to something like this, you'll Out of memory ,so Or more isEmpty() Well !

Make complaints : Great people look at the problem , The cook looked at the solution , I have to search the solution of this kind of dishes Deque How to use !!! Type it first and understand , Then type again after understanding

 

版权声明
本文为[Xiao Qin doesn't lose his hair]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231847462774.html