• 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;
        }
    };

  • 相关阅读:
    centos7安装rabbitmq 总结
    python第六十三天-- 第十一周作业
    python第六十一天,第六十二天 redis
    python第六十天-----RabbitMQ
    python第五十四天--第十周作业
    python第五十三天--进程,协程.select.异步I/O...
    python第五十二天---第九周作业 类 Fabric 主机管理程序
    python第五十一天----线程,Event,队列
    Python基础之-面向对象编程(引言)
    Python中的模块
  • 原文地址:https://www.cnblogs.com/yanqi110/p/5112842.html
Copyright © 2020-2023  润新知