My python code works on my machine

  • 0

    on my machine, I can get '01', but OJ gets '10', can anyone help?

    class Solution:
        # @param A a list of integers
        # @return nothing, sort in place
        def sortColors(self, A):
            length = len (A)
            p = 0
            for i in range(length):
                if A[p]==0:
                    A=[0] + A[:p]+A[p+1:]
                elif A[p]==2:
                    A= A[:p]+A[p+1:]+[2]
            return A
    sol = Solution()
    print sol.sortColors([1,0])

  • 2

    The problem asks to "sort in place". I guess that means to modify one or two elements in the list A in each step, instead of reconstructing the list A.

    After removing the last two lines and modifying another two lines from you codes, the submission is accepted.
    The two lines modified are:

    1. Replace the line A=[0] + A[:p]+A[p+1:]
      with del A[p] and A.insert(0,0) .
    1. Replace the line A= A[:p]+A[p+1:]+[2] with
      del A[p] and A.append(2) .

  • 0

    Hi ncchen, thanks a lot!

  • 0

    vRussell, you're welcome!

Log in to reply

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