美文网首页
LeetCode 394 Decode String 递归方法

LeetCode 394 Decode String 递归方法

作者: nzo | 来源:发表于2018-03-15 09:01 被阅读0次
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;
    }
};

相关文章

网友评论

      本文标题:LeetCode 394 Decode String 递归方法

      本文链接:https://www.haomeiwen.com/subject/mrycqftx.html