LeetCode224(2021.3.10)

LC224. 基本计算器

class Solution {
    public int calculate(String s) {
        Deque<Integer> ops = new LinkedList<Integer>();
        ops.push(1);
        int sign = 1;
        int res = 0;
        int n = s.length();
        int i = 0;
        while (i < n) {
            if (s.charAt(i) == ' ') {
                i++;
            }
            else if (s.charAt(i) == '+') {
                sign = ops.peek();
                i++;
            }
            else if (s.charAt(i) == '-') {
                sign = -ops.peek();
                i++;
            }
            else if (s.charAt(i) == '(') {
                ops.push(sign);
                i++;
            }
            else if (s.charAt(i) == ')') {
                ops.pop();
                i++;
            }
            else {
                long num = 0;
                while (i < n && Character.isDigit(s.charAt(i))) {
                    num = num * 10 + s.charAt(i) - '0';
                    i++;
                }
                res += sign * num;
            }
        }
        return res;
    }
}

发表评论