-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path71.simplify-path.js
More file actions
41 lines (38 loc) · 896 Bytes
/
71.simplify-path.js
File metadata and controls
41 lines (38 loc) · 896 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode id=71 lang=javascript
*
* [71] Simplify Path
*/
// @lc code=start
/**
* @param {string} path
* @return {string}
*/
var simplifyPath = function (path) {
let elements = path.split("/");
let resultArray = [],
result = "/";
for (let i = 0; i < elements.length; i++) {
const element = elements[i];
if (element !== "" && element !== ".") {
if (element === "..") {
resultArray.length ? resultArray.pop() : "";
} else {
resultArray.push(element);
}
}
}
if (!resultArray.length) {
return result;
} else {
for (let i = 0; i < resultArray.length; i++) {
result += resultArray[i] + "/";
}
}
result = result.slice(0, result.length - 1);
return result;
}; // "/home//foo/../..././bar"
// @lc code=end
// @after-stub-for-debug-begin
module.exports = simplifyPath;
// @after-stub-for-debug-end