-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode71.java
More file actions
60 lines (52 loc) · 2.12 KB
/
LeetCode71.java
File metadata and controls
60 lines (52 loc) · 2.12 KB
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
import java.util.Deque;
import java.util.ArrayDeque;
public class LeetCode71 {
public static void main(String[] args) {
// 输入:path = "/home/"
// 输出:"/home"
// 解释:注意,最后一个目录名后面没有斜杠。
System.out.println(new Solution71().simplifyPath("/home/"));
// 输入:path = "/../"
// 输出:"/"
// 解释:从根目录向上一级是不可行的,因为根目录是你可以到达的最高级。
System.out.println(new Solution71().simplifyPath("/../"));
// 输入:path = "/home//foo/"
// 输出:"/home/foo"
// 解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
System.out.println(new Solution71().simplifyPath("/home//foo/"));
// 输入:path = "/a/./b/../../c/"
// 输出:"/c"
// System.out.println(new Solution71().simplifyPath("/a/./b/../../c/"));
// 输入:path = "/a/../../b/../c//.//"
// 输出:"/c"
System.out.println(new Solution71().simplifyPath("/a/../../b/../c//.//"));
// 输入:path = "/a//b////c/d//././/.."
// 输出:"/a/b/c"
System.out.println(new Solution71().simplifyPath("/a//b////c/d//././/.."));
// 输入:path = "/hello../world"
// 输出:"/hello../world"
System.out.println(new Solution71().simplifyPath("/hello../world"));
}
}
class Solution71 {
public String simplifyPath(String path) {
String[] paths = path.split("/");
Deque<String> stack = new ArrayDeque<String>();
for (int i = 0; i < paths.length; i++) {
if (paths[i].equals("..")) {
if (!stack.isEmpty()) {
stack.pop();
}
} else if (paths[i].equals(".") || paths[i].equals("")) {
} else {
stack.push(paths[i]);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append("/");
sb.append(stack.pollLast());
}
return sb.length() == 0 ? "/" : sb.toString();
}
}