class Solution:
# @param {string[]} tokens
# @return {integer}
def __init__(self):
self.operators = {
'+': lambda y, x: x + y,
'': lambda y, x: x  y,
'*': lambda y, x: x * y,
'/': lambda y, x: int(operator.truediv(x, y))
}
def evalRPN(self, tokens):
if not tokens:
return 0
stack = []
for token in tokens:
if token in self.operators:
stack.append(self.operators[token](stack.pop(), stack.pop()))
else:
stack.append(int(token))
return stack[0]
A simple Python solution  O(n) 72ms


said in A simple Python solution  O(n) 72ms:
class Solution:
# @param {string[]} tokens
# @return {integer}
def init(self):
self.operators = {
'+': lambda y, x: x + y,
'': lambda y, x: x  y,
'*': lambda y, x: x * y,
'/': lambda y, x: int(operator.truediv(x, y))
}def evalRPN(self, tokens): if not tokens: return 0 stack = [] for token in tokens: if token in self.operators: stack.append(self.operators[token](stack.pop(), stack.pop())) else: stack.append(int(token)) return stack[0]
why you write operators dict in initial rather than in our method or in the class ?

@weiheng4 said in A simple Python solution  O(n) 72ms:
elf):
self.operators = {
'+': lambda y, x: x + y,
'': lambda y, x: x  y,
'*': lambda y, x: xSo that if evalRPN() is called multiple times, the operators need not be reinitialized each time.