Skip to content

Commit a7b6e1f

Browse files
refactor 71
1 parent edba15a commit a7b6e1f

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.fishercoder.solutions;
22

3-
43
import java.util.Arrays;
54
import java.util.Deque;
65
import java.util.HashSet;
76
import java.util.LinkedList;
87
import java.util.Set;
98

109
/**
11-
* Given an absolute path for a file (Unix-style), simplify it.
10+
* 70. Climbing Stairs
11+
12+
Given an absolute path for a file (Unix-style), simplify it.
1213
1314
For example,
1415
path = "/home/", => "/home"
@@ -22,21 +23,22 @@
2223
*/
2324
public class _71 {
2425

26+
public static class Solution1 {
2527
public String simplifyPath(String path) {
26-
Deque<String> stack = new LinkedList<>();
27-
Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", ""));
28-
for (String dir : path.split("/")) {
29-
if (dir.equals("..") && !stack.isEmpty()) {
30-
stack.pop();
31-
} else if (!skipSet.contains(dir)) {
32-
stack.push(dir);
33-
}
28+
Deque<String> stack = new LinkedList<>();
29+
Set<String> skipSet = new HashSet<>(Arrays.asList("..", ".", ""));
30+
for (String dir : path.split("/")) {
31+
if (dir.equals("..") && !stack.isEmpty()) {
32+
stack.pop();
33+
} else if (!skipSet.contains(dir)) {
34+
stack.push(dir);
3435
}
35-
String result = "";
36-
for (String dir : stack) {
37-
result = "/" + dir + result;
38-
}
39-
return result.isEmpty() ? "/" : result;
36+
}
37+
String result = "";
38+
for (String dir : stack) {
39+
result = "/" + dir + result;
40+
}
41+
return result.isEmpty() ? "/" : result;
4042
}
41-
43+
}
4244
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._71;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _71Test {
10+
private static _71.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _71.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("/home", solution1.simplifyPath("/home/"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("/c", solution1.simplifyPath("/a/./b/../../c/"));
25+
}
26+
}

0 commit comments

Comments
 (0)