-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecodingString.java
More file actions
49 lines (44 loc) · 1.55 KB
/
DecodingString.java
File metadata and controls
49 lines (44 loc) · 1.55 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @description:
* @Author: JachinDo
* @Date: 2019/10/21 20:45
*/
public class DecodingString {
public String decodeString(String s) {
Stack<String> stack = new Stack<>();
for(int i = 0; i < s.length(); i++){
char curr = s.charAt(i);
if (curr != ']') {
stack.push(String.valueOf(curr));
} else {
StringBuilder sb = new StringBuilder();
while(!"[".equals(stack.peek())){
sb.append(stack.pop());
}
stack.pop();
StringBuilder Num = new StringBuilder();
while (!stack.isEmpty() && stack.peek().charAt(0) >= '0' && stack.peek().charAt(0) <= '9') {
Num.append(stack.pop());
}
int repeatCount = Integer.parseInt(Num.reverse().toString());
String repeateString = sb.toString();
for (int j = 0; j < repeatCount; j++) {
// for (int k = 0; k < repeateString.length(); k++) {
// stack.push(String.valueOf(repeateString.charAt(k)));
// }
stack.push(repeateString);
}
}
}
StringBuilder res = new StringBuilder();
while (!stack.isEmpty()) {
res.append(stack.pop());
}
String resu = res.reverse().toString();
System.out.println(resu);
return resu;
}
}