forked from MisterBooo/LeetCodeAnimation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.java
More file actions
34 lines (32 loc) · 1.03 KB
/
1.java
File metadata and controls
34 lines (32 loc) · 1.03 KB
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 res = new StringBuilder();
int multi = 0;
Stack<Integer> stack_multi = new Stack();
Stack<String> stack_res = new Stack();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ('[' == c){
stack_multi.push(multi);
stack_res.push(res.toString());
multi = 0;
res = new StringBuilder();
}
else if (']' == c) {
StringBuilder tmp = new StringBuilder();
int cur_multi = stack_multi.pop();
for (int j = 0; j < cur_multi; j++){
tmp.append(res);
}
res = new StringBuilder(stack_res.pop() + tmp);
}
else if(c >= '0' && c <= '9'){
multi = multi * 10 + (c - '0');
}
else{
res.append(c);
}
}
return res.toString();
}
}