Python Easy Solution Beat 98%


  • 0
    D
    class Solution(object):
        def evalRPN(self, tokens):
            """
            :type tokens: List[str]
            :rtype: int
            """
            ops = {'+': lambda x,y: x+y, '-': lambda x,y: x-y, '*': lambda x,y: x*y, '/': lambda x,y: x/y if x*y>=0 else -(abs(x)/abs(y))}
            stack = []
            for x in tokens:
                if x in ops:
                    second = stack.pop()
                    first = stack.pop()
                    stack.append(ops[x](first, second))
                else:
                    stack.append(int(x))
                
            return stack[0]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.