Skip to content

Commit 00ba644

Browse files
author
tanfanhua
committed
simplify
1 parent 7f036c5 commit 00ba644

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.blankj.easy._071;
2+
3+
import java.util.LinkedList;
4+
5+
public class Solution {
6+
7+
public static void main(String[] args) {
8+
Solution s = new Solution();
9+
System.out.println(s.simplifyPath("/a/b/c/"));
10+
System.out.println(s.simplifyPath("/home//foo/"));
11+
System.out.println(s.simplifyPath("/a/./b/../../c/"));
12+
System.out.println(s.simplifyPath("/a/../../b/../c//.//"));
13+
System.out.println(s.simplifyPath("/a//b////c/d//././/.."));
14+
}
15+
16+
public String simplifyPath(String path) {
17+
if (path == null || path == "") {
18+
return "";
19+
}
20+
String[] dirs = path.split("/");
21+
LinkedList<String> simplifyDirs = new LinkedList<>();
22+
for (String dir : dirs) {
23+
if ("..".equals(dir)) {
24+
if (!simplifyDirs.isEmpty()) {
25+
simplifyDirs.removeLast();
26+
}
27+
} else if (!"".equals(dir) && !".".equals(dir)) {
28+
simplifyDirs.add(dir);
29+
}
30+
}
31+
StringBuilder res = new StringBuilder("");
32+
for (String dir : simplifyDirs) {
33+
res.append("/");
34+
res.append(dir);
35+
}
36+
37+
return res.toString();
38+
}
39+
}

0 commit comments

Comments
 (0)