We use a stack to store operands. When we encounter an operator, we pop the top two operands, apply the operator, and push the result back onto the stack.
- Initialize an empty stack.
- Iterate through each token in the input:
- If token is an operator:
- Pop
b(the second operand). - Pop
a(the first operand). - Perform the operation
a operator b. - Push the result back to stack. (For division, use
int(a / b)to truncate towards zero).
- Pop
- Else (operand):
- Push the integer value of the token.
- If token is an operator:
- Return the only element remaining in the stack.
- Time Complexity: O(N).
- Space Complexity: O(N).
def eval_rpn(tokens):
stack = []
for token in tokens:
if token in "+-*/":
b = stack.pop()
a = stack.pop()
if token == '+':
stack.append(a + b)
elif token == '-':
stack.append(a - b)
elif token == '*':
stack.append(a * b)
else:
# Truncate towards zero
stack.append(int(a / b))
else:
stack.append(int(token))
return stack[0]