394. Decode String

Given an encoded string, return it's decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won't be input like 3a or 2[4].

Examples:

s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2[c]]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".

题意:给出一个加密的字符串,求出原字符串。加密规则如下:k[encoded_string]代表此处有k次重复的encoded_string。注意:当k=1时,数字1和[]省略不写。

思路一:递归实现,利用[]将字符串每部分进行递归处理即可。详见代码:

class Solution {
public:
    string decodeString(string s) {
        int i=0;
        string str;
        // 含有字符串s为空的情况注意特判。 
        if(s.length()==0)
            return "";
        while(i<s.length()){
            long long num=0;
            int cnt=0;
            //这里处理只出现1次的字母 
            if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
                str+=s[i++];
                continue;
            }
            //这里处理 出现多次的字母串,先计算其出现次数num 
            string s1;
            while(s[i]!='['){
                num=num*10+int(s[i]-'0');
                i++;
            }
            i++;
            cnt=1;
            // 求出该出现num 次的字符串 
            while(!(cnt==1&&s[i]==']')){
                if(s[i]=='[')
                     cnt++;
                if(s[i]==']')
                    cnt--;
                s1+=s[i];
                i++;
            }
            //  递归num次 s1字符串即可。 
            for(long long j=0;j<num;j++){
                str+=decodeString(s1);
            }
            i++;
        }
        return str;
    }
};

思路二:用两个栈实现。

class Solution {
public:
    string decodeString(string s) {
           stack<int>num;  //存储子串的个数  
           stack<string>str;  //存储的子串  
           
           string result,t;
           int cnt=0;
           for(int i=0;i<s.length();i++){
            printf("%d\n",i);
               if(isdigit(s[i])){      //若为数字 进行计数 
                   cnt=cnt*10+s[i]-'0';
            }else if(isalpha(s[i])){  //字母,加到当前子串上  这里t为当前[]内的字母组成的字串 
                t+=s[i];
            }else if(s[i]=='['){    //左括号,前一个子串处理完毕,将其入栈准备处理下一个[]字串
                num.push(cnt);
                cnt=0;
                str.push(t);
                t="";
            }else {                    //为右括号,t为两个[]之间的子串,要将其重复ans次  
                int ans=num.top();
                num.pop();
                result=str.top();
                str.pop();
                while(ans--){
                    result+=t;
                    str.top()+=t;       // str.top当前[]层次外的字串。 
                }
                t=result;              //t为外层[]的字串 如 a3[bc]    t=abcbcbc 
            }
        }
        return t;
     }
}; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值