Leetcode Basic Calculator 系列

很经典的stack应用,为了省事儿,不再自己写stack了。

Leetcode #224 Basic Calculator

Implement a basic calculator to evaluate a simple expression string.

The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .

You may assume that the given expression is always valid.

Some examples:

"1 + 1" = 2
" 2-1 + 2 " = 3
"(1+(4+5+2)-3)+(6+8)" = 23

Note: Do not use the eval built-in library function.


class Solution {
public:
    int calculate(string s){
        s += ')';
        stack<int> nums;
        stack<char> opts;
        int index = 0;
        opts.push('(');
        //nums.push(0);   //对于空字符串的处理
        while (index < s.size()) {
            char c = s[index++];
            if (isSpace(c)) continue;
            else if (isOpt(c)) {
                char pre_c = opts.top();
                while (priority(pre_c, c) == 1){
                    int second = nums.top(); nums.pop();
                    int first = nums.top(); nums.pop();
                    opts.pop();
                    switch(pre_c){
                        case '+' :
                          nums.push(first + second);  break;
                        case '-':
                          nums.push(first - second);  break;
                    }
                    pre_c = opts.top();
                }
                if (priority(pre_c, c) == 0) opts.pop();
                else opts.push(c);
            }
            else{
                int n = ctoi(c);
                while(isNum(s[index]))
                    n = n * 10 + ctoi(s[index++]);
                nums.push(n);
            }
        }
        return nums.top();
    }
private:
    bool isSpace(char c){
        return c == ' ';
    }
    bool isOpt(char c){
        return c == '+' || c == '-' || c == '(' || c == ')';
    }
    bool isNum(char c){
        return c >= '0' && c <= '9';
    }
    int ctoi(char c){
        return c - '0' + 0;
    }
    int priority(char c1, char c2){
        if (c1 == '(')
            if(c2 == ')')  return 0;
            else return -1;
        if(c2 == '(') return -1;
        return 1;
    }
};

Leetcode227 Basic Calculator II

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces. The integer division should truncate toward zero.

You may assume that the given expression is always valid.

Some examples:

"3+2*2" = 7
" 3/2 " = 1
" 3+5 / 2 " = 5

Note: Do not use the eval built-in library function.

class Solution {
public:
    int calculate(string s){
        s += '#';
        stack<int> nums;
        stack<char> opts;
        int index = 0;
        opts.push('#');
       // nums.push(0);   //对于空字符串的处理
        while (index < s.size()) {
            char c = s[index++];
            if (isSpace(c)) continue;
            else if (isOpt(c)) {
                char pre_c = opts.top();
                while (priority(pre_c, c)){
                    int second = nums.top(); nums.pop();
                    int first = nums.top(); nums.pop();
                    opts.pop();
                    switch(pre_c){
                        case '*' :
                          nums.push(first * second);  break;
                        case '/' :
                          nums.push(first / second);  break;
                        case '+' :
                          nums.push(first + second);  break;
                        case '-':
                          nums.push(first - second);  break;
                    }
                    pre_c = opts.top();
                }
                opts.push(c);
            }
            else{
                int n = ctoi(c);
                while(isNum(s[index]))
                    n = n * 10 + ctoi(s[index++]);
                nums.push(n);
            }
        }
        return nums.top();
    }
private:
    bool isSpace(char c){
        return c == ' ';
    }
    bool isOpt(char c){
        return c == '+' || c == '-' || c == '*' || c == '/' || c== '#';
    }
    bool isNum(char c){
        return c >= '0' && c <= '9';
    }
    int ctoi(char c){
        return c - '0' + 0;
    }
    bool priority(char c1, char c2){
        if (c1 == '#') return false;
        if (c2 == '#') return true;
        if (c1 == '*' || c1 == '/') return true;
        if (c1 == '+' || c1 == '-')
            if (c2 == '*' || c2 =='/') return false;
            else return true;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值