Leetcode 224. Basic Calculator (计算器递归好题)

  1. Basic Calculator
    Hard
    Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.

Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().

Example 1:

Input: s = “1 + 1”
Output: 2
Example 2:

Input: s = " 2-1 + 2 "
Output: 3
Example 3:

Input: s = “(1+(4+5+2)-3)+(6+8)”
Output: 23

Constraints:

1 <= s.length <= 3 * 105
s consists of digits, ‘+’, ‘-’, ‘(’, ‘)’, and ’ '.
s represents a valid expression.
‘+’ is not used as a unary operation (i.e., “+1” and “+(2 + 3)” is invalid).
‘-’ could be used as a unary operation (i.e., “-1” and “-(2 + 3)” is valid).
There will be no two consecutive operators in the input.
Every number and running calculation will fit in a signed 32-bit integer.

解法1:参考labuladong
注意:

  1. helper()里面的index必须用&,因为递归回来后,index的值就已经递增了。
  2. 递归完后,c=s[index]是更新后的index对应的字符。
  3. 如果不是最后一个字符,遇到运算符(非数字又非空格),要进switch()处理。如果是最后一个字符,不管是什么(空格也好,数字也好),也要进switch()处理,不然这最后一个数字就丢掉了。
class Solution {
public:
    int calculate(string s) {
        int index = 0;
        return helper(s, index);
    }
private:
    int helper(string &s, int &index) {
        stack<int> stk;
        int sLen = s.size();
        char sign = '+';
        int num = 0;
        int res = 0;
        for (; index < sLen; index++) {
            char c = s[index];
            if (c == '(') {
                index++;
                num = helper(s, index);
            }
            c = s[index];
            if (isdigit(c)) {
                num = num * 10 + (c - '0');
            } 
            if ((!isdigit(c) && c != ' ') || index == sLen - 1) { //(c == '+' || c == '-' || c == '*' || c == '/') or reach the end
                switch (sign) {
                    case '+':
                        stk.push(num);
                        break;
                    case '-':
                        stk.push(-num);
                        break;
                    case '*':
                        stk.push(stk.top() * num);
                        stk.pop();
                        break;
                    case '/':
                        stk.push(stk.top() / num);
                        stk.pop();
                        break;
                    default:
                    break;
                }
                sign = c;
                num = 0;
            }
            if (c == ')') {
                index++;
                break;
            }
        }
        
        while (!stk.empty()) {
            res += stk.top();
            stk.pop();
        }
        return res;
    }
};

你可能感兴趣的:(leetcode,linux,算法,数据结构)