zoukankan      html  css  js  c++  java
  • 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;
        }
    };
  • 相关阅读:
    1.vue-1
    7.Docker -- 虚拟服务器
    11.Django -- 中间件
    10.Django -- csrf -- 文件上传
    9.djang -- cookie和session
    8.Django --Ajax
    YOLO v1原理详解
    带你一文读懂Faster RCNN论文
    Week13
    Week12
  • 原文地址:https://www.cnblogs.com/asenyang/p/9815235.html
Copyright © 2011-2022 走看看