• leetcode227


    class Solution {
    public:
        stack<int> OPD;
        stack<char> OPR;
        int calculate(int num1, int num2, char opr){
            switch (opr){
            case '+':{
                         return num1 + num2;
            }
            case '-':{
                         return num1 - num2;
            }
            case '*':{
                         return num1*num2;
            }
            case '/':{
                         return num1 / num2;
            }
            }
        }
        int calculate(string s) {
            int len = s.size();
            int i=0;
            while(i<len){
                if (s[i] == ' '){
                    i++;
                }
                else if (s[i] == '+' || s[i] == '-' || s[i]=='*' || s[i]=='/'){
                    OPR.push(s[i]);
                    i++;
                }
                else{
                    int num = 0;
                    while (i < len && s[i] >= '0' && s[i] <= '9'){
                        num = num * 10 + (s[i] - '0');
                        i++;
                    }
                    if (!OPR.empty() && (OPR.top()=='/' || OPR.top()=='*')){
                        num = calculate(OPD.top(),num,OPR.top());
                        OPR.pop();
                        OPD.pop();
                    }
                    OPD.push(num);
                }
            }
            int res=0;
            while (!OPR.empty()){
                int tmp=OPD.top();
                char ch=OPR.top();
                if(ch=='-'){
                    tmp=-tmp;
                }
                res+=tmp;
                OPD.pop();
                OPR.pop();
            }
            res+=OPD.top();
            return res;
        }
    };
  • 相关阅读:
    TCP/IP面试要点浅析
    Mysql优化策略
    Mysql集群方案简介
    Mysql主从复制
    UML序列图总结(Loop、Opt、Par和Alt)
    Java之注解
    mac:查看端口号被占用情况
    C#监控U盘插拔
    QMap
    js统计对象的层数
  • 原文地址:https://www.cnblogs.com/asenyang/p/9815235.html
Copyright © 2020-2023  润新知