Skip to content

Commit 4a0ada9

Browse files
committed
Create: 71-Simplify-Path.ts
1 parent aa1fbd6 commit 4a0ada9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

typescript/71-Simplify-Path.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function simplifyPath(path: string): string {
2+
const stack: string[] = [];
3+
let cur = '';
4+
5+
for (const c of path + '/') {
6+
if (c === '/') {
7+
if (cur === '..') {
8+
if (stack.length > 0) {
9+
stack.pop();
10+
}
11+
} else if (cur != '' && cur != '.') {
12+
stack.push(cur);
13+
}
14+
cur = '';
15+
} else {
16+
cur += c;
17+
}
18+
}
19+
20+
return '/' + stack.join('/');
21+
}

0 commit comments

Comments
 (0)