【算法】【棧】棧相關題目

字符串解碼

題目鏈接:https://leetcode-cn.com/problems/decode-string/

class Solution {
public:
    string decodeString(string s) {
        //用一個數字棧 保存數字
        //一個string棧 保存str
        //遍歷字符串
        //  遇到數字計算 sum
        //  遇到左括號 把數字壓棧
        //  遇到字母 ch
        //  遇到右括號  用來追加結果

    
        //實現一個能夠使用分配律的計算器
        //3[a2[c]b] -> 3[accb] -> accbaccbaccb
        int n = s.size();
        stack<int> st_num;
        stack<string> st_ch;

        string res = "";
        int num = 0;
        for(int i = 0; i < n; i++)
        {
            char ch = s[i];
            if(ch >= '0' && ch <= '9') num = num * 10 + ch - '0';
            //將[前的數字壓入數字棧中,字母字符串壓入字符串中
            else if(ch == '[') 
            {
                st_num.push(num);
                num = 0;
                st_ch.push(res);
                res.clear();
            }
            else if((ch >= 'a' && ch <= 'z' ) || (ch >= 'A' && ch <= 'Z' ))
                res += ch;
            //遇到] 操作跟它相匹配的[之間的字符串,使用分配律
            else if(ch == ']')
            {
                int cnt = st_num.top();
                st_num.pop();

                for(int i = 0; i < cnt; i++)
                    st_ch.top() += res;
               
                res = st_ch.top(); //之後若還是字母,就會直接加入到res之後
                cout << res << " ";
                //之後若是左括號,res會被壓入字符棧中,作爲上一層的運算
                st_ch.pop();
            }
        }
        return res;
    }
};

基本計算器(不帶括號)

題目鏈接:https://leetcode-cn.com/problems/basic-calculator-ii/

class Solution {
public:
    int calculate(string s) {
        int n = s.size();

        stack<long> st_nums;
        
        long sum = 0;
        char op = '+';
        for(int i = 0; i < n; i++)
        {
            char ch = s[i];
            if(ch >= '0') sum = sum * 10 + ch - '0';
            //如果是最後一位數字時,計算上一個符號的結果併入棧
            if((ch < '0' && ch != ' ') || i == n - 1)
            {
                int pre;
                switch(op)
                {
                    //當前符號爲加減的時候,下一個元素以正負整數的形式直接入棧
                    case '+':
                        st_nums.push(sum);
                        break;
                    case '-':
                        st_nums.push(-sum);
                        break;
                    //當前符號爲乘除的時候,下一個元素與棧頂元素計算結果後入棧頂
                    case '*':
                        pre = st_nums.top();
                        st_nums.pop();
                        st_nums.push(pre * sum);
                        break;
                    case '/':
                        pre = st_nums.top();
                        st_nums.pop();
                        st_nums.push(pre / sum);
                        break;
                }
                op = ch; //更新符號
                sum = 0; //數字清零
            }
        }

        int res = 0;
        //棧不空,棧中的元素相加就是結果
        while(!st_nums.empty())
        {
            res += st_nums.top();
            st_nums.pop();
        }
        return res;
    }
};

基本計算器(帶括號)

題目鏈接:https://leetcode-cn.com/problems/basic-calculator/

class Solution {
public:
    int calculate(string s) {
        stack<char> op;
        stack<int> num;
        for (int i = 0; i < s.size(); i ++ ) {
            char c = s[i];
            if (c == ' ') continue;
            if (c == '+' || c == '-' || c == '(') op.push(c);
            else if (c == ')') {
                op.pop();
                if (op.size() && op.top() != '(') {
                    calc(op, num);
                }
            }
            else {
                int j = i;
                while (j < s.size() && isdigit(s[j])) j ++ ;
                num.push(atoi(s.substr(i, j - i).c_str()));
                i = j - 1;
                if (op.size() && op.top() != '(') {
                    calc(op, num);
                }
            }
        }
        return num.top();
    }
    void calc(stack<char> &op, stack<int> &num) {
        int y = num.top();
        num.pop();
        int x = num.top();
        num.pop();
        if (op.top() == '+') num.push(x + y);
        else num.push(x - y);
        op.pop();
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章