150. Evaluate Reverse Polish Notation

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()
            

你可能感兴趣的:(150. Evaluate Reverse Polish Notation)