码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode() Basic Calculator 不知道哪里错了

时间:2016-01-08 13:15:13      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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 不知道哪里错了

标签:

原文地址:http://www.cnblogs.com/yanqi110/p/5112842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!