We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f9a3c8f commit 24b44c2Copy full SHA for 24b44c2
README.md
@@ -312,6 +312,7 @@ All solutions will be accepted!
312
|299|[Bulls And Cows](https://leetcode-cn.com/problems/bulls-and-cows/description/)|[java/py/js](./algorithms/BullsAndCows)|Medium|
313
|394|[Decode String](https://leetcode-cn.com/problems/decode-string/description/)|[java/py/js](./algorithms/DecodeString)|Medium|
314
|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|
316
317
# Database
318
|#|Title|Solution|Difficulty|
algorithms/SimplifyPath/README.md
@@ -0,0 +1,2 @@
1
+# Simplify Path
2
+We can solve this problem by stack
algorithms/SimplifyPath/Solution.java
@@ -0,0 +1,23 @@
+class Solution {
+ 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
18
+ sb.append(stack.pollLast());
19
20
21
+ return sb.toString();
22
23
+}
algorithms/SimplifyPath/solution.js
@@ -0,0 +1,16 @@
+/**
+ * @param {string} path
+ * @return {string}
+ */
+var simplifyPath = function(path) {
+ let stack = []
+ path.split('/').forEach(part => {
+ if (part != '' && part != '.' && part != '..')
+ stack.push(part)
+ else if (part == '..' && stack.length > 0)
+ stack.pop()
+ })
+ return '/' + stack.join('/')
+};
algorithms/SimplifyPath/solution.py
@@ -0,0 +1,14 @@
+class Solution(object):
+ def simplifyPath(self, path):
+ """
+ :type path: str
+ :rtype: str
+ stack = []
+ for part in path.split('/'):
+ if part != '' and part != '.' and part != '..':
+ stack.append(part)
+ elif part == '..' and len(stack) > 0:
+ return '/' + '/'.join(stack)
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments