zoukankan      html  css  js  c++  java
  • 【算法】【栈】栈相关题目

    字符串解码

    题目链接: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();
        }
    };
    
    
  • 相关阅读:
    struts2之JSP与Action的关系
    struts2之基本配置
    Android笔记之BroadCast判断网络状况
    Android笔记之监听左右滑动事件
    Android笔记之转到主线程运行
    springboot项目启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no embedde
    centos6 初次安装成功,未显示eth0网卡的信息
    layerui上传文件
    判断当前浏览器类型
    jquery重置下拉框
  • 原文地址:https://www.cnblogs.com/Trevo/p/13572675.html
Copyright © 2011-2022 走看看