Whi I got wraong answer?? working on the sort colors.


  • -1
    M

    Hi All,

    I am working on the sort colors question, which is basically sorting the integer array with only 3 values: 0, 1 and 2. The following code got me "Wrong Answer". However, I did got right answer when I tested it in my python environment.

    Here are the error message and my code. Hope somebody can help me out. thanks.

    Error Message: Wrong Answer

    Input:[1,0]
    Output: [1,0]
    Correct output: [0,1]

    Note: When I input [1,0] in my code, I did get the right output of [0,1]. I do not know why the leetcode get the wrong answer??

    My python code.

    class Solution:
    # @param A a list of integers
    # @return nothing, sort in place
    def sortColors(self, A):
    snum = []
    zeroR = 0

        for item in A:
            if (item<1): 
                snum= [item] + snum
                zeroR += 1
            elif (item>1):   
                snum= snum + [item]
            else:
                snum.insert(zeroR,item)
        
        return(snum)

  • 0
    M
    This post is deleted!

Log in to reply
 

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