Movatterモバイル変換


[0]ホーム

URL:


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

Commit0ae5426

Browse files
authored
Create 0071-simplify-path.js
Solved simplify-path in JS.
1 parent5f4f024 commit0ae5426

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎javascript/0071-simplify-path.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Stack
3+
* https://leetcode.com/problems/simplify-path/
4+
*
5+
* Time O(n) | Space O(n)
6+
*@param {string} path
7+
*@return {string}
8+
*/
9+
varsimplifyPath=function(path){
10+
letcurrunt='';
11+
letmyStack=[];
12+
path='/'+path+'/';
13+
for(leti=0;i<path.length;i++){
14+
15+
console.log(myStack);
16+
if(path[i]==='/'){
17+
if(currunt=='..'){
18+
if(myStack.length){
19+
myStack.pop();
20+
}
21+
}elseif(currunt!==''&&currunt!=='.'){
22+
myStack.push(currunt);
23+
}
24+
currunt='';
25+
}else{
26+
currunt+=path[i];
27+
}
28+
}
29+
30+
myStack=myStack.join('/');
31+
myStack='/'+myStack;
32+
returnmyStack;
33+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp