File tree 5 files changed +56
-0
lines changed
5 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -312,6 +312,7 @@ All solutions will be accepted!
312
312
| 299| [ Bulls And Cows] ( https://leetcode-cn.com/problems/bulls-and-cows/description/ ) | [ java/py/js] ( ./algorithms/BullsAndCows ) | Medium|
313
313
| 394| [ Decode String] ( https://leetcode-cn.com/problems/decode-string/description/ ) | [ java/py/js] ( ./algorithms/DecodeString ) | Medium|
314
314
| 938| [ Range Sum Of Bst] ( https://leetcode-cn.com/problems/range-sum-of-bst/description/ ) | [ java/py/js] ( ./algorithms/RangeSumOfBst ) | Medium|
315
+ | 71| [ Simplify Path] ( https://leetcode-cn.com/problems/simplify-path/description/ ) | [ java/py/js] ( ./algorithms/SimplifyPath ) | Medium|
315
316
316
317
# Database
317
318
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Simplify Path
2
+ We can solve this problem by stack
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String simplifyPath (String path ) {
3
+ LinkedList <String > stack = new LinkedList <String >();
4
+ StringBuilder sb = new StringBuilder ();
5
+
6
+ for (String part : path .split ("/" )) {
7
+ if (!part .equals ("" ) && !part .equals ("." ) && !part .equals (".." ))
8
+ stack .push (part );
9
+ else if (part .equals (".." ) && stack .size () > 0 )
10
+ stack .pop ();
11
+ }
12
+
13
+ if (stack .size () == 0 )
14
+ sb .append ("/" );
15
+
16
+ while (stack .size () > 0 ) {
17
+ sb .append ("/" );
18
+ sb .append (stack .pollLast ());
19
+ }
20
+
21
+ return sb .toString ();
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } path
3
+ * @return {string }
4
+ */
5
+ var simplifyPath = function ( path ) {
6
+ let stack = [ ]
7
+
8
+ path . split ( '/' ) . forEach ( part => {
9
+ if ( part != '' && part != '.' && part != '..' )
10
+ stack . push ( part )
11
+ else if ( part == '..' && stack . length > 0 )
12
+ stack . pop ( )
13
+ } )
14
+
15
+ return '/' + stack . join ( '/' )
16
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def simplifyPath (self , path ):
3
+ """
4
+ :type path: str
5
+ :rtype: str
6
+ """
7
+ stack = []
8
+ for part in path .split ('/' ):
9
+ if part != '' and part != '.' and part != '..' :
10
+ stack .append (part )
11
+ elif part == '..' and len (stack ) > 0 :
12
+ stack .pop ()
13
+
14
+ return '/' + '/' .join (stack )
You can’t perform that action at this time.
0 commit comments