Why does my solution get an error:"Do not return anything, sort A in-place instead."


  • 0
    T

    When the input is [0], the error is "Do not return anything, sort A in-place instead." But it can be accepted in IDE.Please help me! Thanks!

    class Solution:
    # @param A a list of integers
    # @return nothing, sort in place
    def sortColors(self, A):
        start = 0
        end = len(A)-1
        i = 0
        while i <= end:
            try:
                while A[start] == 0:
                    start += 1
                    i = start
    
                while A[end] == 2:
                    end -= 1
            except:
                return A
    
            if i > end:
                return A
    
            if A[i] == 0:
                A[start],A[i] = A[i],A[start]
                start += 1
            if A[i] == 2:
                A[end],A[i] = A[i],A[end]
                end -= 1
            if A[i] == 1:
                i += 1
    
        return A

  • 0

    Because you don't have to return the array, you have to swap the elements in the input array to have it sorted


  • 0
    R

    True. On the other hand, the problem description does not contain "in place" at all. That is a defect in the description. Hope the admin can fix that. @1337c0d3r


Log in to reply
 

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