Cannot pass the test and got "wrong answer" but it looks good on my local


  • 0
    D
    >>> class Solution:
        # @param tokens, a list of string
        # @return an integer
        def evalRPN(self, tokens):
            list_of_operator = [ "+", "-", "*", "/" ]
            first_num = 0
            second_num = 0
            result = int(tokens[0])
            num_count = 0
            buffer_list = []
            def cal(num1,num2,op):
                if op == "+":
                    return int(num1+num2)
                if op == "-":
                    return int(num1-num2)
                if op == "*":
                    return int(num1*num2)
                if op == "/":
                    return int(num1/num2)
            while tokens != []:
                i=tokens.pop(0)
                if i in list_of_operator:
                    second_num = buffer_list.pop()
                    first_num = buffer_list.pop()
                    result = cal(int(first_num), int(second_num), i)
                    buffer_list.append(result)
                # put the number in if it's not an operator
                else:
                    buffer_list.append(i)
            if len(buffer_list) == 1:
                result = buffer_list[0]
                return int(result)
            else:
                print('error')
                return -1
    
            
    >>> test=Solution()
    >>> test.evalRPN(["10","6","9","3","+","-11","*","/","*","17","+","5","+"])
    22
    

    OJ:
    Input: ["10","6","9","3","+","-11","","/","","17","+","5","+"]
    Output: 12
    Expected: 22


Log in to reply
 

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