class Solution {
public:
string find(string& patten,int i,int &n){ //i是一對(duì)[]開始的位置,n是返回值,一對(duì)[]結(jié)束位置
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;
}
};
LeetCode 394 Decode String 遞歸方法
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 問題: Given an encoded string, return it's decoded string.T...
- My code: 看答案寫的。 reference:https://discuss.leetcode.com/to...
- Given an encoded string, return it's decoded string. Desc...
- Given an encoded string, return it's decoded string.The e...
- 折半查找,又稱作二叉搜索,是面試時(shí)經(jīng)常問到的算法問題,今天我們來(lái)看一下它的非遞歸和遞歸方法. 在有序表中,把待查找...