LeetCode 150 Evaluate Reverse Polish Notation - Implementation

The approach

Push numbers, pop two and compute on operators.

function evalRPN(tokens): stack = [] for token in tokens: if token in "+-/": b = stack.pop() a = stack.pop() if token == '+': stack.push(a + b) elif token == '-': stack.push(a - b) elif token == '': stack.push(a * b) else: stack.push(int(a / b)) else: stack.push(int(token)) return stack[0]

O(n)O(n) time, O(n)O(n) space.