150. Evaluate Reverse Polish Not
2016-09-16 本文已影响0人
阿团相信梦想都能实现
import operator
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack=[]
operators={"+":operator.add,"-":operator.sub,"*":operator.mul,"/":operator.div}
for item in tokens:
if item not in operators:
stack.append(int(item))
else:
b,a=stack.pop(),stack.pop()
stack.append(int(operators[item](a*1.0,b)))
return stack.pop()