Correct answer in my machine but wrong in OJ


  • 0
    Y

    The expected output of the case ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] is 22, and the answer in my machine is also 22, but the output of the test is 12.

    I don't know what's wrong.

    This is my code:

    class Solution:
    # @param tokens, a list of string
    # @return an integer
    def evalRPN(self, tokens):
        stack=[]
        for s in tokens:
            if s=='+':
                op1=stack.pop()
                op2=stack.pop()
                res=op1+op2
                stack.append(res)
            elif s=='-':
                op1=stack.pop()
                op2=stack.pop()
                res=op2-op1
                stack.append(res)
            elif s=='*':
                op1=stack.pop()
                op2=stack.pop()
                res=op2*op1
                stack.append(res)
            elif s=='/':
                op1=stack.pop()
                op2=stack.pop()
                res=op2/op1
                stack.append(int(res))
            else:
                stack.append(int(s))
        return stack.pop()

  • 2
    Y

    Okay, finally got it. In python2, 6/-132 would be -1, but it's expected to be 0. And in python3, 6/(-132) would be -0.045454545454545456, so int(6/(-132)) is 0 in python3.


  • 0
    D

    you can find the more useful information from here.
    http://stackoverflow.com/questions/19517868/integer-division-by-negative-number


Log in to reply
 

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