点击打开链接
class Solution {
public:
string dfs(string s, int &k){
string ans;
int cnt=0;
while(k<s.size()){
if(isdigit(s[k])) cnt = cnt*10+(s[k++]-'0');
else if(s[k]=='['){
string tem = dfs(s, ++k);
for(int i=0; i<cnt; i++) ans+=tem;
cnt =0;
}
else if(s[k]==']'){
k++;
return ans;
}
else ans+=s[k++];
}
return ans;
}
string decodeString(string s) {
int k=0;
return dfs(s, k);
}
};
