栈
- 394. 字符串解码
- 730.统计不同回文子序列
394. 字符串解码
我自己写的方法
class Solution {
public String decodeString(String s) {
char[] chs = s.toCharArray();
LinkedList<Character> stack = new LinkedList<>();
for(char ch:chs){
if(ch==']'){
stack = helper(stack);
}else{
stack.push(ch);
}
}
//stack变成字符串
String str ="";
while(!stack.isEmpty()){
str = stack.pop()+str;
}
return str;
}
public LinkedList<Character> helper( LinkedList<Character> stack){
ArrayList<Character> list = new ArrayList<>();
while(stack.peek()!='['){
list.add(stack.pop());
}
stack.pop();
//取数字
String a="";
while(!stack.isEmpty()&&stack.peek()>='0'&&stack.peek()<='9'){
a = stack.pop()+a;
}
int ans = Integer.parseInt(a);
for(int i=0;i<ans;i++){
for(int j = list.size()-1;j>=0;j--){
stack.push(list.get(j));
}
}
return stack;
}
}