C++ simple and clear solution


  • 7
    L
    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.push_back(c);                
                }
                else if(c == '[') {
                    chars.push(res);
                    nums.push(num);
                    res = "";
                    num = 0;
                }
                else if(c == ']') {
                    string tmp = res;
                    for(int i = 0; i < nums.top()-1; ++i) {
                        res += tmp;
                    }
                    res = chars.top() + res;
                    chars.pop(); nums.pop();
                }
            }
            return res;
         }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.