My accepted Python solution.


  • 2
    Z
     class Solution:
    
        def __init__(self):
            self.operator = {
                '+': '__add__',
                '-': '__sub__',
                '*': '__mul__',
                '/': '__div__',
            }
    
        # @param tokens, a list of string
        # @return an integer
        def evalRPN(self, tokens):
            stack = []
            for t in tokens:
                if t in ('+', '-', '*'):
                    op2 = stack.pop()
                    op1 = stack.pop()
                    operator = getattr(op1, self.operator[t])
                    stack.append(operator(op2))
                elif t == '/':
                    op2 = stack.pop()
                    op1 = stack.pop()
                    operator = getattr(float(op1), self.operator[t])
                    stack.append(int(operator(op2)))
                else:
                    stack.append(int(t))
            return stack[0]
    

    One of the most significant points is that Python division will round the number towards negative infinity. Which means

    -1 / 2 == -1
    

    It's different from C/C++ and Java.


  • 0
    Z

    -1 / 2 == -1

    int(-1.0 / 2.0) == 0


Log in to reply
 

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