class Solution {
public:
string decodeString(string s) {
//使用两个栈,数字与字符
stack<int> stk_num;
stack<string> stk_str;
int current_num = 0;
string current_str; //当前括号内的字符串
for(char c:s){
if(isdigit(c)){
current_num = current_num*10 + c - '0'; //char 转 数字
}else if(c == '['){
//遇到左括号,入栈,稍后再处理
stk_num.push(current_num);
current_num = 0;
stk_str.push(current_str);
current_str.clear();
}else if(c == ']'){
//遇到右括号,出栈结清一次
string strprev = stk_str.top();
stk_str.pop();
string newstring;
for(int i=0; i<stk_num.top(); i++){ //重复
newstring = current_str+newstring;
}
stk_num.pop();
current_str = strprev + newstring;
}else{
current_str = current_str + c; //记录当前括号字符
}
}
return current_str;
}
};