Skip to content

Commit bfe1eb6

Browse files
committed
add: Simplify Path
1 parent 145b988 commit bfe1eb6

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3333
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
3434
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
3535
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
36+
37+
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/) | [JavaScript](./src/simplify-path/res.js)|Medium|
3638
|73|[Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [JavaScript](./src/set-matrix-zeroes/res.js)|Medium|
3739
|75|[Sort Colors](https://leetcode.com/problems/sort-colors/) | [JavaScript](./src/sort-colors/res.js)|Medium|
3840
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/) | [JavaScript](./src/decode-ways/res.js)|Medium|

src/simplify-path/res.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* res.js
3+
* @authors Joe Jiang ([email protected])
4+
* @date 2017-05-11 09:59:27
5+
*
6+
* @param {string} path
7+
* @return {string}
8+
*/
9+
let simplifyPath = function(path) {
10+
let len = path.length,
11+
res = [];
12+
13+
for (let i=0; i<len;) {
14+
let tmp = '';
15+
while (path.charAt(i) === '/' && i<len) i++;
16+
while (path.charAt(i) !== '/' && i<len) {
17+
tmp += path.charAt(i);
18+
i++;
19+
}
20+
21+
if (tmp === '..') res.pop();
22+
else if (tmp !== '.' && tmp !== '') res.push(tmp);
23+
}
24+
25+
if (res.length === 0) return '/';
26+
else return '/' + res.join('/');
27+
};

0 commit comments

Comments
 (0)