File tree Expand file tree Collapse file tree 4 files changed +21
-96
lines changed
solution/0000-0099/0071.Simplify Path Expand file tree Collapse file tree 4 files changed +21
-96
lines changed Original file line number Diff line number Diff line change @@ -248,30 +248,18 @@ function simplifyPath(path: string): string {
248
248
249
249
``` rust
250
250
impl Solution {
251
- #[allow(dead_code)]
252
251
pub fn simplify_path (path : String ) -> String {
253
- let mut s : Vec <& str > = Vec :: new ();
254
-
255
- // Split the path
256
- let p_vec = path . split (" /" ). collect :: <Vec <& str >>();
257
-
258
- // Traverse the path vector
259
- for p in p_vec {
260
- match p {
261
- // Do nothing for "" or "."
262
- "" | " ." => {
263
- continue ;
264
- }
252
+ let mut stk = Vec :: new ();
253
+ for s in path . split ('/' ) {
254
+ match s {
255
+ "" | " ." => continue ,
265
256
" .." => {
266
- if ! s . is_empty () {
267
- s . pop ();
268
- }
257
+ stk . pop ();
269
258
}
270
- _ => s . push (p ),
259
+ _ => stk . push (s ),
271
260
}
272
261
}
273
-
274
- " /" . to_string () + & s . join (" /" )
262
+ " /" . to_string () + & stk . join (" /" )
275
263
}
276
264
}
277
265
```
@@ -307,22 +295,4 @@ public class Solution {
307
295
308
296
<!-- solution: end -->
309
297
310
- <!-- solution: start -->
311
-
312
- ### 方法二
313
-
314
- <!-- tabs: start -->
315
-
316
- #### Go
317
-
318
- ``` go
319
- func simplifyPath (path string ) string {
320
- return filepath.Clean (path)
321
- }
322
- ```
323
-
324
- <!-- tabs: end -->
325
-
326
- <!-- solution: end -->
327
-
328
298
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -246,30 +246,18 @@ function simplifyPath(path: string): string {
246
246
247
247
``` rust
248
248
impl Solution {
249
- #[allow(dead_code)]
250
249
pub fn simplify_path (path : String ) -> String {
251
- let mut s : Vec <& str > = Vec :: new ();
252
-
253
- // Split the path
254
- let p_vec = path . split (" /" ). collect :: <Vec <& str >>();
255
-
256
- // Traverse the path vector
257
- for p in p_vec {
258
- match p {
259
- // Do nothing for "" or "."
260
- "" | " ." => {
261
- continue ;
262
- }
250
+ let mut stk = Vec :: new ();
251
+ for s in path . split ('/' ) {
252
+ match s {
253
+ "" | " ." => continue ,
263
254
" .." => {
264
- if ! s . is_empty () {
265
- s . pop ();
266
- }
255
+ stk . pop ();
267
256
}
268
- _ => s . push (p ),
257
+ _ => stk . push (s ),
269
258
}
270
259
}
271
-
272
- " /" . to_string () + & s . join (" /" )
260
+ " /" . to_string () + & stk . join (" /" )
273
261
}
274
262
}
275
263
```
@@ -305,22 +293,4 @@ public class Solution {
305
293
306
294
<!-- solution: end -->
307
295
308
- <!-- solution: start -->
309
-
310
- ### Solution 2
311
-
312
- <!-- tabs: start -->
313
-
314
- #### Go
315
-
316
- ``` go
317
- func simplifyPath (path string ) string {
318
- return filepath.Clean (path)
319
- }
320
- ```
321
-
322
- <!-- tabs: end -->
323
-
324
- <!-- solution: end -->
325
-
326
296
<!-- problem: end -->
Original file line number Diff line number Diff line change 1
1
impl Solution {
2
- #[ allow( dead_code) ]
3
2
pub fn simplify_path ( path : String ) -> String {
4
- let mut s: Vec < & str > = Vec :: new ( ) ;
5
-
6
- // Split the path
7
- let p_vec = path. split ( "/" ) . collect :: < Vec < & str > > ( ) ;
8
-
9
- // Traverse the path vector
10
- for p in p_vec {
11
- match p {
12
- // Do nothing for "" or "."
13
- "" | "." => {
14
- continue ;
15
- }
3
+ let mut stk = Vec :: new ( ) ;
4
+ for s in path. split ( '/' ) {
5
+ match s {
6
+ "" | "." => continue ,
16
7
".." => {
17
- if !s. is_empty ( ) {
18
- s. pop ( ) ;
19
- }
8
+ stk. pop ( ) ;
20
9
}
21
- _ => s . push ( p ) ,
10
+ _ => stk . push ( s ) ,
22
11
}
23
12
}
24
-
25
- "/" . to_string ( ) + & s. join ( "/" )
13
+ "/" . to_string ( ) + & stk. join ( "/" )
26
14
}
27
15
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments