File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .LinkedList ;
2
+ import java .util .List ;
3
+
4
+ /**
5
+ * @author: wangjunchao(王俊超)
6
+ * @time: 2018-09-28 15:50
7
+ **/
8
+ public class Solution {
9
+ public String simplifyPath (String path ) {
10
+ String [] ss = path .split ("/" );
11
+ List <String > result = new LinkedList <>();
12
+
13
+ for (String part : ss ) {
14
+ if ("." .equals (part )) {
15
+ continue ;
16
+ } else if (".." .equals (part )) {
17
+ if (result .size () > 0 ) {
18
+ result .remove (result .size () - 1 );
19
+ }
20
+ } else if (!"" .equals (part .trim ())) {
21
+ result .add (part );
22
+ }
23
+ }
24
+
25
+ StringBuilder builder = new StringBuilder ();
26
+ for (String part : result ) {
27
+ builder .append ("/" ).append (part );
28
+ }
29
+
30
+ return builder .length () == 0 ? "/" : builder .toString ();
31
+ }
32
+ }
You can’t perform that action at this time.
0 commit comments