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

LeetCode() Basic Calculator 不知道哪里错了的更多相关文章

  1. [LeetCode] Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  2. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  3. LeetCode Basic Calculator II

    原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/ Implement a basic calculator to evaluate ...

  4. LeetCode Basic Calculator

    原题链接在这里:https://leetcode.com/problems/basic-calculator/ Implement a basic calculator to evaluate a s ...

  5. [LeetCode] Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. [LeetCode] Basic Calculator IV 基本计算器之四

    Given an expression such as expression = "e + 8 - a + 5" and an evaluation map such as {&q ...

  7. [LeetCode] Basic Calculator & Basic Calculator II

    Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression ...

  8. LeetCode——Basic Calculator II

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

  9. LeetCode——Basic Calculator

    Description: Implement a basic calculator to evaluate a simple expression string. The expression str ...

随机推荐

  1. C# unity3d 贪吃蛇 游戏 源码 及其感想

    这个游戏的设计过程是这样的: 1,创建

  2. android小技巧(二)

    一.如何控制Android  LED等?(设置NotificationManager的一些参数) 代码如下: final int ID_LED=19871103; NotificationManage ...

  3. mysql 大小写问题-sql-mode问题

    一.mysql 字段名 表名 数据库名 是否区分大小写 今天碰到数据库大小写问题,linux与windows下问题 同时又碰到保留字 http://www.cnblogs.com/lawdong/ar ...

  4. C语言文法分析

    程序 → <外部声明>|<程序><外部声明> <外部声明> → <函数定义> | <声明> <函数定义> → < ...

  5. PAT 1072. Gas Station (30)

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  6. ubuntu 下更新PHP版本 ZT

    sudo add-apt-repository ppa:ondrej/php5 如果报错 add-apt-repository: command not found add-apt-repositor ...

  7. PHP面向对象实例(图形计算器)

    效果:

  8. jieba中文分词(python)

    问题小结 1.安装 需要用到python,根据python2.7选择适当的安装包.先下载http://pypi.python.org/pypi/jieba/ ,解压后运行python setup.py ...

  9. Mac下用brew安装nginx

    1. nginx nginx [engine x] is an HTTP and reverse proxy server, a mail proxy server, and a generic TC ...

  10. LayaAir引擎——(四)

    LayaAir引擎 TiledMap 使用 所需要的软件: Tiled地图编辑器 版本0.16.2 LayaAir IDE 所需要的图片:图块图片(1.png) 步骤1: 文件->新文件-> ...