Skip to content

Commit 0abd2c1

Browse files
committed
feat: add question 71
1 parent 16e9d9e commit 0abd2c1

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

71.data

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"/home/"
2+
"/../"
3+
"/home//foo/"
4+
"/a/./b/../../c/"
5+
"/a/../../b/../c//.//"
6+
"/a//b////c/d//././/.."

71.简化路径.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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

0 commit comments

Comments
 (0)