forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
27 lines (24 loc) · 637 Bytes
/
res.js
File metadata and controls
27 lines (24 loc) · 637 Bytes
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
/**
* res.js
* @authors Joe Jiang (hijiangtao@gmail.com)
* @date 2017-05-11 09:59:27
*
* @param {string} path
* @return {string}
*/
let simplifyPath = function(path) {
let len = path.length,
res = [];
for (let i=0; i<len;) {
let tmp = '';
while (path.charAt(i) === '/' && i<len) i++;
while (path.charAt(i) !== '/' && i<len) {
tmp += path.charAt(i);
i++;
}
if (tmp === '..') res.pop();
else if (tmp !== '.' && tmp !== '') res.push(tmp);
}
if (res.length === 0) return '/';
else return '/' + res.join('/');
};