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 {
248248
249249``` rust
250250impl Solution {
251- #[allow(dead_code)]
252251 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 ,
265256 " .." => {
266- if ! s . is_empty () {
267- s . pop ();
268- }
257+ stk . pop ();
269258 }
270- _ => s . push (p ),
259+ _ => stk . push (s ),
271260 }
272261 }
273-
274- " /" . to_string () + & s . join (" /" )
262+ " /" . to_string () + & stk . join (" /" )
275263 }
276264}
277265```
@@ -307,22 +295,4 @@ public class Solution {
307295
308296<!-- solution: end -->
309297
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-
328298<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -246,30 +246,18 @@ function simplifyPath(path: string): string {
246246
247247``` rust
248248impl Solution {
249- #[allow(dead_code)]
250249 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 ,
263254 " .." => {
264- if ! s . is_empty () {
265- s . pop ();
266- }
255+ stk . pop ();
267256 }
268- _ => s . push (p ),
257+ _ => stk . push (s ),
269258 }
270259 }
271-
272- " /" . to_string () + & s . join (" /" )
260+ " /" . to_string () + & stk . join (" /" )
273261 }
274262}
275263```
@@ -305,22 +293,4 @@ public class Solution {
305293
306294<!-- solution: end -->
307295
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-
326296<!-- problem: end -->
Original file line number Diff line number Diff line change 11impl Solution {
2- #[ allow( dead_code) ]
32 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 ,
167 ".." => {
17- if !s. is_empty ( ) {
18- s. pop ( ) ;
19- }
8+ stk. pop ( ) ;
209 }
21- _ => s . push ( p ) ,
10+ _ => stk . push ( s ) ,
2211 }
2312 }
24-
25- "/" . to_string ( ) + & s. join ( "/" )
13+ "/" . to_string ( ) + & stk. join ( "/" )
2614 }
2715}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments