zoukankan      html  css  js  c++  java
  • LeetCode() Basic Calculator 不知道哪里错了

    class Solution {
    public:
        int calculate(string s) {
            stack<int> num;
            stack<char> symbol;
            for(int i=0;i<s.length();i++){
                if(s[i]==' ')   continue;
                else if(s[i]=='('||s[i]=='+'||s[i]=='-')  symbol.push(s[i]);
                else if(s[i]>='0'&&s[i]<='9'){
                    for(int j=i+1;j<s.length();j++){
                        if(s[j]<'0'||s[j]>'9'){
                            num.push(stoi(s.substr(i,j-i)));
                            i=j-1;
                            break;
                        }
                           
                    }
                }
                else if(s[i]==')'){
                    stack<int> tem_i;
                    stack<char> tem_c;
                    while(symbol.top()!='('){
                        tem_i.push(num.top());
                        num.pop();
                        tem_c.push(symbol.top());
                        symbol.pop();
                    }
                    symbol.pop();
                    while(!tem_c.empty()){
                        char c=tem_c.top();
                        tem_c.pop();
                        if(c=='+'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a+b);
                        }
                        if(c=='-'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a-b);
                        }
                    }
                    num.push(tem_i.top());
                    tem_i.pop();
                }
            }
            stack<int> tem_i;
            stack<char> tem_c;
            while(!symbol.empty()){
                tem_i.push(num.top());
                num.pop();
                tem_c.push(symbol.top());
                symbol.pop();
            }
            while(!tem_c.empty()){
                        char c=tem_c.top();
                        tem_c.pop();
                        if(c=='+'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a+b);
                        }
                        if(c=='-'){
                            int a=tem_i.top();
                            tem_i.pop();
                            int b=tem_i.top();
                            tem_i.pop();
                            tem_i.push(a-b);
                        }
            }
            return tem_i.top();
        }
        int stoi(string s){
            int res=0;
            for(int i=0;i<s.length();i++){
                res=res*10+(s[i]-'0');
            }
            return res;
        }
    };

  • 相关阅读:
    mysql 快速生成百万条测试数据
    DEV SIT UAT
    云计算的三层SPI模型
    go的下载
    redis主从 哨兵
    Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)
    oracle创建表空间
    mycat源码分析
    js判断是否是数字通用写法
    spring aop获取目标对象的方法对象(包括方法上的注解)
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5112842.html
Copyright © 2011-2022 走看看