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 24b44c2

Browse files
committed
solve problem Simplify Path
1 parent f9a3c8f commit 24b44c2

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ All solutions will be accepted!
312312
|299|[Bulls And Cows](https://leetcode-cn.com/problems/bulls-and-cows/description/)|[java/py/js](./algorithms/BullsAndCows)|Medium|
313313
|394|[Decode String](https://leetcode-cn.com/problems/decode-string/description/)|[java/py/js](./algorithms/DecodeString)|Medium|
314314
|938|[Range Sum Of Bst](https://leetcode-cn.com/problems/range-sum-of-bst/description/)|[java/py/js](./algorithms/RangeSumOfBst)|Medium|
315+
|71|[Simplify Path](https://leetcode-cn.com/problems/simplify-path/description/)|[java/py/js](./algorithms/SimplifyPath)|Medium|
315316

316317
# Database
317318
|#|Title|Solution|Difficulty|

‎algorithms/SimplifyPath/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Simplify Path
2+
We can solve this problem by stack
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String simplifyPath(String path) {
3+
LinkedList<String> stack = new LinkedList<String>();
4+
StringBuilder sb = new StringBuilder();
5+
6+
for (String part : path.split("/")) {
7+
if (!part.equals("") && !part.equals(".") && !part.equals(".."))
8+
stack.push(part);
9+
else if (part.equals("..") && stack.size() > 0)
10+
stack.pop();
11+
}
12+
13+
if (stack.size() == 0)
14+
sb.append("/");
15+
16+
while (stack.size() > 0) {
17+
sb.append("/");
18+
sb.append(stack.pollLast());
19+
}
20+
21+
return sb.toString();
22+
}
23+
}

‎algorithms/SimplifyPath/solution.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} path
3+
* @return {string}
4+
*/
5+
var simplifyPath = function(path) {
6+
let stack = []
7+
8+
path.split('/').forEach(part => {
9+
if (part != '' && part != '.' && part != '..')
10+
stack.push(part)
11+
else if (part == '..' && stack.length > 0)
12+
stack.pop()
13+
})
14+
15+
return '/' + stack.join('/')
16+
};

‎algorithms/SimplifyPath/solution.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def simplifyPath(self, path):
3+
"""
4+
:type path: str
5+
:rtype: str
6+
"""
7+
stack = []
8+
for part in path.split('/'):
9+
if part != '' and part != '.' and part != '..':
10+
stack.append(part)
11+
elif part == '..' and len(stack) > 0:
12+
stack.pop()
13+
14+
return '/' + '/'.join(stack)

0 commit comments

Comments
(0)

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