leetcode 71. Simplify Path(简化路径)

Given a string path, which is an absolute path (starting with a slash ‘/’) to a file or directory in a Unix-style file system, convert it to the simplified canonical path.

In a Unix-style file system, a period ‘.’ refers to the current directory, a double period ‘…’ refers to the directory up a level, and any multiple consecutive slashes (i.e. ‘//’) are treated as a single slash ‘/’. For this problem, any other format of periods such as ‘…’ are treated as file/directory names.

The canonical path should have the following format:

The path starts with a single slash ‘/’.
Any two directories are separated by a single slash ‘/’.
The path does not end with a trailing ‘/’.
The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period ‘.’ or double period ‘…’)
Return the simplified canonical path.

Example 1:

Input: path = “/home/”
Output: “/home”
Explanation: Note that there is no trailing slash after the last directory name.
Example 2:

Input: path = “/…/”
Output: “/”
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
Example 3:

Input: path = “/home//foo/”
Output: “/home/foo”

简单来说就是linux下路径简化问题。

思路:
路径简化有以下几种情况:

  1. 多个" / “可简化为一个” / "。
  2. " … “表示回到上一路径,比如”/home/leetcode/…“就是”/home", 这相当于把上一个leetcode给pop出来了。
  3. “.“表示当前路径,比如”/home/leetcode/.”, 那就是"/home/leetcode",可以看到"."没有任何作用,可以跳过
  4. 如果路径为空,认为就在根目录下,即" / "。

根据上面的情况,可以用" / “来split路径,提取split出来的字符串,然后用”/“连接这些字符串。
其中,”.“的字符串直接跳过,”…“就把前一个字符串pop掉,空字符串跳过(多个”/"简化为1个)。

    public String simplifyPath(String path) {
        Stack<String> stack = new Stack();
        StringBuilder result = new StringBuilder();
        String[] folder = path.split("/");
        
        for(int i = 0; i < folder.length; i++) {
            if(!stack.isEmpty() && folder[i].equals("..")) {
                stack.pop();
            }else if(!folder[i].equals("") && !folder[i].equals(".") &&
                   !folder[i].equals("..")){
                stack.push(folder[i]);
            }
        }
        
        if(stack.isEmpty()) return "/";
        
        while(!stack.isEmpty()) {
            result.insert(0, stack.pop());
            result.insert(0, "/");
        }
        return result.toString();
    }

用数组模拟stack

    public String simplifyPath(String path) {
        int n = path.length();
        String[] folders = path.split("/");
        String[] st = new String[n]; //stack
        int head = -1;   //栈顶
        StringBuilder sb = new StringBuilder();

        for(String folder : folders) {
            if(folder.equals("/") || folder.equals(".") || folder.equals("")) continue;
            if(folder.equals("..")) {
                if(head >= 0) head --;
            } else st[++head] = folder;
        }
        
        if(head == -1) return "/";

        for(int i = 0; i <= head; i++) {
            sb.append("/");
            sb.append(st[i]);
        }
        return sb.toString();
    }

    //文件夹有几种情况
    //'/':'///'split出来的,不做操作,跳过
    //"",split出来的第一个符号,需要跳过
    //‘..',上一级,stack.pop
    //'.',当前文件夹,不做操作
    //其他,文件夹名,stack.push

你可能感兴趣的:(leetcode,leetcode,算法,数据结构)