LintCode 980 · Basic Calculator II (计算器,栈好题)

980 · Basic Calculator II
Algorithms
Medium
Description
Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . Division of integers should round off decimals.

You may assume that the given expression is always valid.
Do not use the eval built-in library function.

Example
Example 1:

Input:
“3+2*2”
Output:
7
Example 2:

Input:
" 3/2 "
Output:
1
Tags
Company
Airbnb
Related Problems

849
Basic Calculator III
Hard

978
Basic Calculator
Medium

解法1:这题相对来说容易一点,因为没有括号。

class Solution {
public:
    /**
     * @param s: the given expression
     * @return: the result of expression
     */
    int calculate(string &s) {
        int index = 0;
        char op = '+';
        int num = 0;
        vector<int> nums;
        while (index < s.size()) {
            if (s[index] == ' ') {
                index++;
                continue;
            }
            while (isdigit(s[index])) {
                num = num * 10 + (s[index] - '0');
                index++;
            } //else { //+ - * /  注意:这里不能用else,不然s最后是数字的话就不会调用下面的代码了。
            
            switch(op) { //note: it is not switch(c) !
                case '+':
                    nums.push_back(num);
                    break;
                case '-':
                    nums.push_back(-num);
                    break;
                case '*':
                    nums.back() *= num;
                    break;
                case '/':
                    nums.back() /= num;
                    break;
            }
            op = s[index];
            num = 0; //这里要清零
            index++;
        }
        int res = 0;
        for (auto n : nums) {
            res += n;
        }
        return res;
    }
};

解法2:也可以先找到优先级最低的字符,如果有并列的,就先处理右边的。为什么不能先处理左边的呢?看看1-2+8,不能处理成1-(2+8)!
比如说”3+2-65“。先找到+号,将其分为"3"和"2-65", 然后再分别递归。

class Solution {
public:
    /**
     * @param s: the given expression
     * @return: the result of expression
     */
    int calculate(string &s) {
        return helper(s, 0, s.size() - 1);
    }
private:
    int helper(string &s, int start, int end) {  //[start, end]
        if (start > end) return 0;
        int posAdd = -1, posMinus = -1, posMulti = -1, posDiv = -1;
        for (int i = end; i >= start; i--) { //注意这里是从后往前找,因为同优先级运算符,先处理靠右的。参考1-2+8,不能处理成1-(2+8)!
            if (s[i] == '+' && posAdd == -1) {
                posAdd = i;
                break; //找到了+-就不用管*/了,不然会超时
            }
            if (s[i] == '-' && posMinus == -1) {
                posMinus = i;
                break; //找到了+-就不用管*/了,不然会超时
            }
            if (s[i] == '*' && posMulti == -1) {
                posMulti = i;
                //break; //这里不能break,因为还可能有+-
            }
            if (s[i] == '/' && posDiv == -1) {
                posDiv = i;
                //break; //这里不能break, 因为还可能有+-
            }
        }
        int num = 0;
        if (posAdd == -1 && posMinus == -1 && posMulti == - 1 && posDiv == -1) {
            for (int i = start; i <= end; i++) {
                if (s[i] == ' ') continue;
                num = num * 10 + (s[i] - '0');
            }
            return num;
        }
        if (posAdd > posMinus) {
            int leftRes = helper(s, start, posAdd - 1);
            int rightRes = helper(s, posAdd + 1, end);
            return leftRes + rightRes;
        } else if (posAdd < posMinus) {
            int leftRes = helper(s, start, posMinus - 1);
            int rightRes = helper(s, posMinus + 1, end);
            return leftRes - rightRes;
        } else if (posMulti > posDiv) {
            int leftRes = helper(s, start, posMulti - 1);
            int rightRes = helper(s, posMulti + 1, end);
            return leftRes * rightRes;
        } else {
            int leftRes = helper(s, start, posDiv - 1);
            int rightRes = helper(s, posDiv + 1, end);
            return leftRes / rightRes;
        }        
    }
};

解法3:先转换成逆波兰,然后evaluate。

你可能感兴趣的:(算法)