-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.js
36 lines (35 loc) · 1019 Bytes
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function (path) {
const stack = [];
let index = 1;
while (index < path.length) {
if (path[index] === '.') {
let count = 1;
index++;
// 找到第一个是/的
while (index < path.length && path[index] !== '/') {
index++;
count++;
}
if (count === 2 && path[index - 1] === '.') {
stack.pop();
} else if (count > 2) {
stack.push(path.slice(index - count, index));
}
} else if (path[index] !== '/') {
const startIndex = index;
index++;
while (index < path.length && path[index] !== '/') {
index++;
}
stack.push(path.slice(startIndex, index));
}
while (index < path.length && path[index] === '/') {
index++;
}
}
return `/${stack.join('/')}`;
};