LeetCode 394 Decode String 递归方法
2018-03-15 本文已影响0人
nzo
class Solution {
public:
string find(string& patten,int i,int &n){ //i是一对[]开始的位置,n是返回值,一对[]结束位置
string decoded="";
string str="";
stringstream s;
int end;
while(1){
if(i>=patten.size()) return decoded;
if(patten[i]=='[') {i++;continue;}
else if(patten[i]==']') {
n=i+1;
break;
}
else if(patten[i]>='0' && patten[i]<='9'){
int count=0;
while(patten[i]>='0' && patten[i]<='9')
{
int d=patten[i]-'0';
count=count*10 + d;
i++;
}
str=find(patten,i+1,end);
for(int j=0;j<count;j++){
decoded+=str;
}
cout<<decoded<<endl;
i=end;
}
else {
decoded+=patten[i];
i++;
}
}
return decoded;
}
string decodeString(string s) {
string ss="";
int a ;
ss=find(s,0,a);
return ss;
}
};