友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

71. Simplify Path

Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.

In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix.

Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

Example 1:
Input: "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.
Example 2:
Input: "/../"
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: "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
Example 4:
Input: "/a/./b/../../c/"
Output: "/c"
Example 5:
Input: "/a/../../b/../c//.//"
Output: "/c"
Example 6:
Input: "/a//b////c/d//././/.."
Output: "/a/b/c"

解题分析

有点想复杂了,只需要一个字符串分割,然后加一个辅助栈即可。

不过,我感觉滑动窗口应该也可以,回头可以试试。

参考资料

Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.

In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix

Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.

Example 1:

Input: "/home/"
Output: "/home"
Explanation: Note that there is no trailing slash after the last directory name.

Example 2:

Input: "/../"
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: "/home//foo/"
Output: "/home/foo"
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.

Example 4:

Input: "/a/./b/../../c/"
Output: "/c"

Example 5:

Input: "/a/../../b/../c//.//"
Output: "/c"

Example 6:

Input: "/a//b////c/d//././/.."
Output: "/a/b/c"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
 * Runtime: 5 ms, faster than 71.14% of Java online submissions for Simplify Path.
 * Memory Usage: 39.9 MB, less than 6.67% of Java online submissions for Simplify Path.
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-02-04 16:15
 */
public String simplifyPath(String path) {
    String[] paths = path.split("/");
    Deque<String> stack = new LinkedList<>();
    for (String subpath : paths) {
        if ("..".equals(subpath) && !stack.isEmpty()) {
            stack.removeLast();
            continue;
        }
        if (subpath.length() > 0 && !".".equals(subpath) && !"..".equals(subpath)) {
            stack.addLast(subpath);
        }
    }
    StringBuilder result = new StringBuilder();
    while (!stack.isEmpty()) {
        String name = stack.removeFirst();
        result.append("/").append(name);
    }
    return result.length() > 0 ? result.toString() : "/";
}

/**
 * Runtime: 3 ms, faster than 97.40% of Java online submissions for Simplify Path.
 * Memory Usage: 39.4 MB, less than 20.00% of Java online submissions for Simplify Path.
 */
public String simplifyPathLoop(String path) {
    if (Objects.isNull(path)) {
        return "/";
    }
    Deque<String> stack = new LinkedList<>();
    int i = 0;
    while (i < path.length()) {
        if (path.charAt(i) == '/') {
            i++;
            continue;
        }
        StringBuilder sb = new StringBuilder();
        while (i < path.length() && (path.charAt(i) != '.' && path.charAt(i) != '/')) {
            sb.append(path.charAt(i));
            i++;
        }

        if (sb.length() > 0) {
            stack.addLast(sb.toString());
            continue;
        }
        while (i < path.length() && path.charAt(i) != '/') {
            sb.append(path.charAt(i));
            i++;
        }
        String strings = sb.toString();
        if (sb.length() == 2 && !stack.isEmpty() && "..".equals(strings)) {
            stack.removeLast();
        }
        if (sb.length() > 2 || (sb.length() == 2 && !"..".equals(strings))) {
            stack.addLast(strings);
        }
    }
    if (stack.isEmpty()) {
        return "/";
    }
    StringBuilder result = new StringBuilder();
    while (!stack.isEmpty()) {
        String name = stack.removeFirst();
        result.append("/").append(name);
    }
    return result.length() > 0 ? result.toString() : "/";
}