leetcode--394. Decode String

题目描述:

leetcode--394. Decode String

使用深度优先遍历方法

class Solution {
public:
    string decodeString(string s) {
        
        string res = "";
        int a = 0;
        return helper(a, s);
        
        
    }
    string helper(int &pos, string s){
        
        int num = 0;
        string word = "";
        for(;pos<s.size();++pos){
            char cur = s[pos];
            if(cur == '['){

               // 如果遇到 “[”  代表进行新的判断
                string p = helper(++pos, s);
                for(;num>0;num--){
                    word+=p;
                }
            }
            else if(cur>='0' && cur<='9'){
                // 因为这里有长度是n位数的
                num = num*10 + cur - '0';
            }
            else if(cur == ']')
                return word;
            else
                word += cur;
        }
        return word;
    }
};