forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (31 loc) · 959 Bytes
/
Solution.java
File metadata and controls
31 lines (31 loc) · 959 Bytes
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
import java.util.*;
public class Solution {
public String simplifyPath(String path) {
String[] layers = path.split("/");
Stack<String> stack = new Stack<String>();
for (String curr : layers) {
if (curr.length() == 0 || curr.equals(".")) {
continue;
} else if (curr.equals("..")) {
if (!stack.isEmpty()) stack.pop();
} else {
stack.push(curr);
}
}
StringBuilder sb = new StringBuilder();
for (String curr : stack) {
sb.append("/");
sb.append(curr);
}
if (sb.length() == 0) {
return "/";
} else {
return sb.toString();
}
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.simplifyPath("/home/"));
System.out.println(s.simplifyPath("/a/./b/../../c/"));
}
}