在碰到’]’的处理整个模式, 数字的处理, 这里是反序的, 是不是正序读, 要通过位数计算当前数字要乘的10^x, 注意反转

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
35
36
37
class Solution {
/**
* 在碰到']'的处理整个模式, 数字的处理, 这里是反序的, 是不是正序读,
* 要通过位数计算当前数字要乘的10^x, 注意反转
*/
public String decodeString(String s) {
StringBuilder ans = new StringBuilder();
Deque<Character> st = new ArrayDeque<>();
for (char ch : s.toCharArray()) {
if (ch != ']') {
st.push(ch);
continue ;
}
StringBuilder bracket = new StringBuilder();
char c;
while ((c = st.pop()) != '[') {
bracket.append(c);
}
int num = 0, p = 0;
while (!st.isEmpty() && Character.isDigit(st.peek())) {
num += (st.pop() - '0') * Math.pow(10, p++);
}
bracket.reverse();
for (int i = 0; i < num; i++) {
for (char b : bracket.toString().toCharArray()) {
st.push(b);
}
}
}

while (!st.isEmpty()) {
ans.append(st.pop());
}

return ans.reverse().toString();
}
}

Comments
Recent Posts
进程树
进程管理
Categories
Website Info
Article Count :
3
Total Word Count :
4.6k
Unique Visitors :
Page Views :
Last Update :