forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
27 lines (27 loc) · 834 Bytes
/
Solution.java
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
class Solution {
public String decodeString(String s) {
Deque<Integer> s1 = new ArrayDeque<>();
Deque<String> s2 = new ArrayDeque<>();
int num = 0;
String res = "";
for (char c : s.toCharArray()) {
if ('0' <= c && c <= '9') {
num = num * 10 + c - '0';
} else if (c == '[') {
s1.push(num);
s2.push(res);
num = 0;
res = "";
} else if (c == ']') {
StringBuilder t = new StringBuilder();
for (int i = 0, n = s1.pop(); i < n; ++i) {
t.append(res);
}
res = s2.pop() + t.toString();
} else {
res += String.valueOf(c);
}
}
return res;
}
}