from typing import Callable
class Solution:
def evalRPN(self, tokens: list[str]) -> int:
stack: list[int] = []
operators: dict[str, Callable[[int, int], int]] = {
'+': lambda a, b: a + b,
'-': lambda a, b: a - b,
'*': lambda a, b: a * b,
'/': lambda a, b: int(a / b),
}
for token in tokens:
if token in operators:
b, a = stack.pop(), stack.pop()
stack.append(operators[token](a, b))
else:
stack.append(int(token))
return stack[0]
150. Evaluate Reverse Polish Notation - LeetCode
ยท One min read