-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-394.java
More file actions
34 lines (33 loc) · 1000 Bytes
/
lc-394.java
File metadata and controls
34 lines (33 loc) · 1000 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
class Solution {
public String decodeString(String s) {
StringBuilder sb = new StringBuilder();
Stack<Integer> cntStack = new Stack<>();
Stack<String> resStack = new Stack<>();
String res = "";
int num = 0;
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
num = 10*num + c - '0';
}
if (Character.isLetter(c)) {
res += c;
}
if (c == '[') {
cntStack.push(num);
resStack.push(res);
num = 0;
res = "";
}
if (c == ']') {
StringBuilder tmp = new StringBuilder();
int counts = cntStack.pop();
for (int i = 0; i < counts; ++i) {
tmp.append(res);
}
res = resStack.pop() + tmp.toString();
num = 0;
}
}
return res;
}
}