leetcode-pp / 91alg-10-daily-check

第X期打卡仓库
8 stars 0 forks source link

写超时了补 #8

Closed Dominique9898 closed 1 year ago

Dominique9898 commented 1 year ago
class Solution {
public:

    bool isDigital(char c) {
        return (c - '0') >= 0 && (c - '9') <= 9;
    }
    string help(string s) {
        int num = 0;
        string res = "";
        stack<int> s1;
        stack<string> s2;
        for (int i = 0; i < s.length(); i++) {
            if (isDigital(s[i])) num = num * 10 + (s[i] - '0');
            else if (s[i] == '[') {
                s1.push(num);
                num = 0;
                s2.push(res); 
                res = "";
            } else if (s[i] == ']') {
                int times = s1.top();
                s1.pop();
                while(times--) s2.top() += res;
                res = s2.top();
                s2.pop();
            } else {
                res = res + s[i];
            }
        }
        return res;
    }

    string decodeString(string s) {
        return help(s);
    }
};