227 Basic Calculator II

Given a string s which represents an expression, evaluate this expression and return its value.

The integer division should truncate toward zero.

You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1].

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

Example 1:

Input: s = "3+2*2"
Output: 7

Example 2:

Input: s = " 3/2 "
Output: 1

Example 3:

Input: s = " 3+5 / 2 "
Output: 5

Constraints:

  • 1 <= s.length <= 3 * 105
  • s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.
  • s represents a valid expression.
  • All the integers in the expression are non-negative integers in the range [0, 231 - 1].
  • The answer is guaranteed to fit in a 32-bit integer.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Solution:
    PRECEDENCE = {
        '*': 2,
        '/': 2,
        '+': 1,
        '-': 1,
    }
    
    OPS = {
        '+': lambda x, y: x + y,
        '-': lambda x, y: x - y,
        '*': lambda x, y: x * y,
        '/': lambda x, y: x // y,
    }
    
    def calculate(self, s: str) -> int:
        # tokenize
        tokens = []
        token = ""
        for c in s:
            if c in self.PRECEDENCE:
                if token:
                    tokens.append(int(token))
                    token = ''
                tokens.append(c)
            else:
                token += c
        if token:
            tokens.append(int(token))
        
        # in-order to post-order
        stack = []
        post_tokens = []
        for t in tokens:
            if t in self.PRECEDENCE:
                while stack and self.PRECEDENCE[stack[-1]] >= self.PRECEDENCE[t]:
                    post_tokens.append(stack.pop())
                stack.append(t)
                continue
            post_tokens.append(t)
        while stack:
            post_tokens.append(stack.pop())
        
        # evaluate
        stack = []
        for t in post_tokens:
            if t in self.PRECEDENCE:
                operand_2 = stack.pop()
                operand_1 = stack.pop()
                stack.append(self.OPS[t](operand_1, operand_2))
            else:
                stack.append(t)
        return stack[0]


'''
Shorter Version (without using stacks)
'''
class Solution:
    def calculate(self, s: str) -> int:
        if len(s) == 0:
            return 0
        current_num = 0
        last_num = 0
        result = 0
        op = '+'
        for i, c in enumerate(s):
            if c.isdigit():
                current_num = current_num * 10 + int(c)
            if not c.isdigit() and c != ' ' or i == len(s)-1:
                if op in '+-':
                    result += last_num
                    last_num = current_num if op == '+' else -current_num
                elif op == '*':
                    last_num *= current_num
                elif op == '/':
                    last_num = int(last_num/current_num)
                op = c
                current_num = 0
        result += last_num
        return result