Skip to content

Commit 0e0d407

Browse files
committed
71. 简化路径
1 parent 169b271 commit 0e0d407

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
|66|[加一](https://leetcode-cn.com/problems/plus-one/)|[JavaScript](./algorithms/plus-one.js)|Easy|
3434
|69|[x 的平方根 ](https://leetcode.cn/problems/sqrtx/)|[JavaScript](./algorithms/sqrtx.js)|Easy|
3535
|70|[爬楼梯](https://leetcode.cn/problems/climbing-stairs/)|[JavaScript](./algorithms/climbing-stairs.js)|Easy|
36+
|71|[简化路径](https://leetcode.cn/problems/simplify-path/)|[JavaScript](./algorithms/simplify-path.js)|Medium|
3637
|73|[矩阵置零](https://leetcode.cn/problems/set-matrix-zeroes/)|[JavaScript](./algorithms/set-matrix-zeroes.js)|Medium|
3738
|82|[删除排序链表中的重复元素 II](https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/)|[JavaScript](./algorithms/remove-duplicates-from-sorted-list-ii.js)|Medium|
3839
|83|[删除排序链表中的重复元素](https://leetcode.cn/problems/remove-duplicates-from-sorted-list/)|[JavaScript](./algorithms/remove-duplicates-from-sorted-list.js)|Easy|

algorithms/simplify-path.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} path
3+
* @return {string}
4+
*/
5+
var simplifyPath = function (path) {
6+
path = path.split("/");
7+
let stack = [];
8+
9+
// 忽略 '.' ''
10+
// 遇到 '..' 则弹出栈顶
11+
12+
for (let char of path) {
13+
if (char === "..") {
14+
stack.pop();
15+
} else if (char && char !== ".") {
16+
stack.push(char);
17+
}
18+
}
19+
20+
return `/${stack.join("/")}`;
21+
};

0 commit comments

Comments
 (0)