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 f7284ab

Browse files
committed
solve problem Decode String
1 parent 282d670 commit f7284ab

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ All solutions will be accepted!
310310
|165|[Compare Version Numbers](https://leetcode-cn.com/problems/compare-version-numbers/description/)|[java/py/js](./algorithms/CompareVersionNumbers)|Medium|
311311
|8|[String To Integer Atoi](https://leetcode-cn.com/problems/string-to-integer-atoi/description/)|[java/py/js](./algorithms/StringToIntegerAtoi)|Medium|
312312
|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|
313314

314315
# Database
315316
|#|Title|Solution|Difficulty|

‎algorithms/DecodeString/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Decode String
2+
We can solve this problem by stack

‎algorithms/DecodeString/Solution.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public String decodeString(String s) {
3+
LinkedList<String> stack = new LinkedList<String>();
4+
StringBuilder sb = new StringBuilder();
5+
6+
for (char c : s.toCharArray()) {
7+
if (c == ']') {
8+
StringBuilder strSb = new StringBuilder();
9+
while (stack.size() > 0 && !stack.peek().equals("["))
10+
strSb.insert(0, stack.pop());
11+
stack.pop();
12+
13+
StringBuilder intSb = new StringBuilder();
14+
while (stack.size() > 0 && stack.peek().matches("\\d+"))
15+
intSb.insert(0, stack.pop());
16+
17+
String temp = strSb.toString();
18+
strSb = new StringBuilder();
19+
for (int i = 0; i < Integer.valueOf(intSb.toString()); i++)
20+
strSb.append(temp);
21+
stack.push(strSb.toString());
22+
} else {
23+
stack.push(String.valueOf(c));
24+
}
25+
}
26+
27+
while (stack.size() > 0)
28+
sb.append(stack.pollLast());
29+
return sb.toString();
30+
}
31+
}

‎algorithms/DecodeString/solution.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @return {string}
4+
*/
5+
var decodeString = function(s) {
6+
let stack = []
7+
8+
s.split('').forEach(c => {
9+
if (c == ']') {
10+
let tempStr = ''
11+
while (stack.length > 0 && stack[stack.length - 1] != '[')
12+
tempStr = stack.pop() + tempStr
13+
stack.pop()
14+
15+
let tempInt = ''
16+
while (stack.length > 0 && /\d+/.test(stack[stack.length - 1]))
17+
tempInt = stack.pop() + tempInt
18+
19+
stack.push(tempStr.repeat(parseInt(tempInt)))
20+
} else
21+
stack.push(c)
22+
})
23+
24+
return stack.join('')
25+
};

‎algorithms/DecodeString/solution.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution(object):
2+
def decodeString(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
stack = []
8+
for c in s:
9+
if c == ']':
10+
temp_str = ''
11+
while len(stack) > 0 and stack[-1] != '[':
12+
temp_str = stack.pop() + temp_str
13+
stack.pop()
14+
15+
temp_int = ''
16+
while len(stack) > 0 and stack[-1].isdigit():
17+
temp_int = stack.pop() + temp_int
18+
19+
stack.append(int(temp_int) * temp_str)
20+
else:
21+
stack.append(c)
22+
23+
return ''.join(stack)

0 commit comments

Comments
(0)

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