Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f675ba1

Browse files
committed
Simplify Path
1 parent e21c1da commit f675ba1

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Source : https://leetcode.com/problems/simplify-path/
2+
// Author : Han Zichi
3+
// Date : 2017年01月23日
4+
5+
/**
6+
* @param {string} path
7+
* @return {string}
8+
*/
9+
var simplifyPath = function(path) {
10+
let res = []
11+
, str = '';
12+
13+
for (let item of path) {
14+
if (item === '/') {
15+
if (str === '' || str === '.') {
16+
// nothing to do
17+
} else if (str === '..') {
18+
if (res.length)
19+
res.pop();
20+
} else {
21+
res.push(str);
22+
}
23+
str = '';
24+
} else {
25+
str += item;
26+
}
27+
}
28+
29+
if (str && str !== '.' && str !== '..')
30+
res.push(str);
31+
else if (str === '..' && res.length)
32+
res.pop();
33+
34+
return '/' + res.join('/');
35+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /