Did someone pass this in Python?


  • 0
    X

    I implement a bucket-sort algorithm in python, but I got an error-output([1, 0]) for the input [0, 1], I am pretty sure that my code is correct because it works well in my computer, so does someone know how to get rid of it?


  • 0
    S

    It would be better to show your code with some explanation here.


  • 0
    H

    yes I did. the test case should be correct.
    btw I thought bucket sort was not in-place sort, which was required by the problem?


  • 0
    T

    I got similar problem that could not pass the test case [1, 0] while my code worked well on my computer, it's weird :(

    class Solution:
    # @param A a list of integers
    # @return nothing, sort in place
    def sortColors(self, A):
        if A != None and len(A) > 1:
            A, one, two = self._sort(A) 
            # one is the place to insert 1, two is the place to insert 2
    def _sort(self, A):
        if len(A) == 1:
            if A[0] == 0:
                return A, 1, 1
            if A[0] == 1:
                return A, 0, 1
            if A[0] == 2:
                return A, 0, 0
        tmp = A[-1]
        A, one, two = self._sort(A[:-1])
        if tmp == 0:
            A.insert(0, tmp)
            return A, one+1, two+1
        if tmp == 1:
            A.insert(one,tmp)
            return A, one, two+1
        if tmp == 2:
            A.insert(two,tmp)
            return A, one, two

  • 0
    S
    This post is deleted!

  • 1
    S

    Have you guys noticed this comment? return nothing, sort in place.

    class Solution:
    # @param A a list of integers
    # @return nothing, sort in place
    def sortColors(self, A):

  • 0
    T

    means A would be modified as sorted list, right?


Log in to reply
 

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