-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_71.java
58 lines (54 loc) · 2.22 KB
/
_71.java
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
package com.fishercoder.solutions.firstthousand;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
public class _71 {
public static class Solution1 {
/*
* For LinkedList class in Java, if you use pop(), then you'll have to use push() as its corresponding method to remove from the "top" of the stack, using pollLast() will not work.
*/
public String simplifyPath(String path) {
Deque<String> stack = new LinkedList<>();
Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", ""));
for (String dir : path.split("/")) {
if (dir.equals("..") && !stack.isEmpty()) {
stack.pop();
} else if (!skipSet.contains(dir)) {
stack.push(dir);
}
}
String result = "";
for (String dir : stack) {
result = "/" + dir + result;
}
return result.isEmpty() ? "/" : result;
}
}
public static class Solution2 {
/*
* This solution doesn't vary too much from the above one, except in that it's using pollLast() and addLast() instead of pop() and push().
* Key notes:
* if using pollLast, then it must be consistent across all calls, including peekLast() and addLast(), cannot mix with pop() and push(), otherwise, unexpected/undesired results will happen.
*/
public String simplifyPath(String path) {
Set<String> skip = new HashSet(Arrays.asList("..", ".", ""));
Deque<String> stack = new LinkedList();
String[] directories = path.split("/");
for (String dir : directories) {
if (dir.equals("..") && !stack.isEmpty()) {
stack.pollLast();
} else if (!skip.contains(dir)) {
stack.addLast(dir);
}
}
StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
sb.append("/");
sb.append(stack.pollFirst());
}
return sb.length() == 0 ? "/" : sb.toString();
}
}
}