File tree 5 files changed +150
-22
lines changed
solution/0000-0099/0071.Simplify Path
5 files changed +150
-22
lines changed Original file line number Diff line number Diff line change 67
67
68
68
<!-- 这里可写通用的实现逻辑 -->
69
69
70
+ 栈实现。
71
+
70
72
<!-- tabs:start -->
71
73
72
74
### ** Python3**
73
75
74
76
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
77
76
78
``` python
77
-
79
+ class Solution :
80
+ def simplifyPath (self , path : str ) -> str :
81
+ stk = []
82
+ for s in path.split(' /' ):
83
+ if not s or s == ' .' :
84
+ continue
85
+ if s == ' ..' :
86
+ if stk:
87
+ stk.pop()
88
+ else :
89
+ stk.append(s)
90
+ return ' /' + ' /' .join(stk)
78
91
```
79
92
80
93
### ** Java**
81
94
82
95
<!-- 这里可写当前语言的特殊实现逻辑 -->
83
96
84
97
``` java
98
+ class Solution {
99
+ public String simplifyPath (String path ) {
100
+ Deque<String > stk = new ArrayDeque<> ();
101
+ for (String s : path. split(" /" )) {
102
+ if (" " . equals(s) || " ." . equals(s)) {
103
+ continue ;
104
+ }
105
+ if (" .." . equals(s)) {
106
+ stk. pollLast();
107
+ } else {
108
+ stk. offerLast(s);
109
+ }
110
+ }
111
+ return " /" + String . join(" /" , stk);
112
+ }
113
+ }
114
+ ```
115
+
116
+ ### ** Go**
117
+
118
+ ``` go
119
+ func simplifyPath (path string ) string {
120
+ var stk []string
121
+ for _ , s := range strings.Split (path, " /" ) {
122
+ if s == " " || s == " ." {
123
+ continue
124
+ }
125
+ if s == " .." {
126
+ if len (stk) > 0 {
127
+ stk = stk[0 : len (stk)-1 ]
128
+ }
129
+ } else {
130
+ stk = append (stk, s)
131
+ }
132
+ }
133
+ return " /" + strings.Join (stk, " /" )
134
+ }
135
+ ```
85
136
137
+ ``` go
138
+ func simplifyPath (path string ) string {
139
+ return filepath.Clean (path)
140
+ }
86
141
```
87
142
88
143
### ** ...**
Original file line number Diff line number Diff line change 62
62
63
63
## Solutions
64
64
65
+ Using stack.
66
+
65
67
<!-- tabs:start -->
66
68
67
69
### ** Python3**
68
70
69
71
``` python
70
-
72
+ class Solution :
73
+ def simplifyPath (self , path : str ) -> str :
74
+ stk = []
75
+ for s in path.split(' /' ):
76
+ if not s or s == ' .' :
77
+ continue
78
+ if s == ' ..' :
79
+ if stk:
80
+ stk.pop()
81
+ else :
82
+ stk.append(s)
83
+ return ' /' + ' /' .join(stk)
71
84
```
72
85
73
86
### ** Java**
74
87
75
88
``` java
89
+ class Solution {
90
+ public String simplifyPath (String path ) {
91
+ Deque<String > stk = new ArrayDeque<> ();
92
+ for (String s : path. split(" /" )) {
93
+ if (" " . equals(s) || " ." . equals(s)) {
94
+ continue ;
95
+ }
96
+ if (" .." . equals(s)) {
97
+ stk. pollLast();
98
+ } else {
99
+ stk. offerLast(s);
100
+ }
101
+ }
102
+ return " /" + String . join(" /" , stk);
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### ** Go**
108
+
109
+ ``` go
110
+ func simplifyPath (path string ) string {
111
+ var stk []string
112
+ for _ , s := range strings.Split (path, " /" ) {
113
+ if s == " " || s == " ." {
114
+ continue
115
+ }
116
+ if s == " .." {
117
+ if len (stk) > 0 {
118
+ stk = stk[0 : len (stk)-1 ]
119
+ }
120
+ } else {
121
+ stk = append (stk, s)
122
+ }
123
+ }
124
+ return " /" + strings.Join (stk, " /" )
125
+ }
126
+ ```
76
127
128
+ ``` go
129
+ func simplifyPath (path string ) string {
130
+ return filepath.Clean (path)
131
+ }
77
132
```
78
133
79
134
### ** ...**
Original file line number Diff line number Diff line change
1
+ func simplifyPath (path string ) string {
2
+ var stk []string
3
+ for _ , s := range strings .Split (path , "/" ) {
4
+ if s == "" || s == "." {
5
+ continue
6
+ }
7
+ if s == ".." {
8
+ if len (stk ) > 0 {
9
+ stk = stk [0 : len (stk )- 1 ]
10
+ }
11
+ } else {
12
+ stk = append (stk , s )
13
+ }
14
+ }
15
+ return "/" + strings .Join (stk , "/" )
16
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String simplifyPath (String path ) {
3
- List <String > dirs = new ArrayList <>();
4
- int dirStart = 0 , len = path .length ();
5
- while (dirStart < len ) {
6
- while (dirStart < len && path .charAt (dirStart ) == '/' ) dirStart ++;
7
- int dirEnd = dirStart ;
8
- while (dirEnd < len && path .charAt (dirEnd ) != '/' ) dirEnd ++;
9
- String dir = path .substring (dirStart , dirEnd );
10
- if (!"." .equals (dir )) {
11
- if (".." .equals (dir )) {
12
- if (! dirs .isEmpty ()) dirs .remove (dirs .size () - 1 );
13
- } else if (dir .length () > 0 ) {
14
- dirs .add (dir );
15
- }
3
+ Deque <String > stk = new ArrayDeque <>();
4
+ for (String s : path .split ("/" )) {
5
+ if ("" .equals (s ) || "." .equals (s )) {
6
+ continue ;
7
+ }
8
+ if (".." .equals (s )) {
9
+ stk .pollLast ();
10
+ } else {
11
+ stk .offerLast (s );
16
12
}
17
- dirStart = ++dirEnd ;
18
- }
19
- StringBuilder sb = new StringBuilder ("/" );
20
- for (int i = 0 ; i < dirs .size (); i ++) {
21
- if (i == dirs .size () - 1 ) sb .append (dirs .get (i ));
22
- else sb .append (dirs .get (i )).append ("/" );
23
13
}
24
- return sb . toString ( );
14
+ return "/" + String . join ( "/" , stk );
25
15
}
26
16
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def simplifyPath (self , path : str ) -> str :
3
+ stk = []
4
+ for s in path .split ('/' ):
5
+ if not s or s == '.' :
6
+ continue
7
+ if s == '..' :
8
+ if stk :
9
+ stk .pop ()
10
+ else :
11
+ stk .append (s )
12
+ return '/' + '/' .join (stk )
You can’t perform that action at this time.
0 commit comments