class Solution {
public:
    string decodeString(string s) {
        stack<string> chars;
        stack<int> nums;
        string res;
        int num = 0;
        
        for (char c: s) {
            if (isdigit(c))
                num = num * 10 + (c - '0');
            else if (isalpha(c)) {
                res += c;
            } else if (c == '[') {
                chars.push(res);
                nums.push(num);
                res = "";
                num = 0;
            } else {
                string tmp = res;
                for (int i = 0; i < nums.top() - 1; i++)
                    res += tmp;
                res = chars.top() + res;
                chars.pop();
                nums.pop();
            }
        }
        
        return res;
    }
};

 

posted on 2018-10-12 09:04  Sempron2800+  阅读(138)  评论(0编辑  收藏  举报