File tree 2 files changed +39
-0
lines changed
2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ "/home/"
2
+ "/../"
3
+ "/home//foo/"
4
+ "/a/./b/../../c/"
5
+ "/a/../../b/../c//.//"
6
+ "/a//b////c/d//././/.."
Original file line number Diff line number Diff line change
1
+ /*
2
+ * @lc app=leetcode.cn id=71 lang=javascript
3
+ *
4
+ * [71] 简化路径
5
+ *
6
+ * 1. 按 / 分词
7
+ * 2. 用栈存结果, 新路径入栈, ..出栈(判空), .不变
8
+ */
9
+
10
+ // @lc code=start
11
+ /**
12
+ * @param {string } path
13
+ * @return {string }
14
+ */
15
+ var simplifyPath = function ( path ) {
16
+ path = path . split ( / \/ + / ) ;
17
+
18
+ const result = [ ] ;
19
+ for ( let i = 0 ; i < path . length ; i ++ ) {
20
+ if ( path [ i ] === '' || path [ i ] === '.' ) {
21
+ continue ;
22
+ } else if ( path [ i ] === '..' ) {
23
+ if ( result . length ) {
24
+ result . pop ( ) ;
25
+ }
26
+ } else {
27
+ result . push ( path [ i ] ) ;
28
+ }
29
+ }
30
+
31
+ return '/' + result . join ( '/' ) ;
32
+ } ;
33
+ // @lc code=end
You can’t perform that action at this time.
0 commit comments