File tree 1 file changed +39
-0
lines changed
src/com/blankj/easy/_0071
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments