Clear solution in Python


  • 0
    B
    class Solution:
        # @param A a list of integers
        # @return nothing, sort in place
        def sortColors(self, A):
            if len(A) < 2:
                return
            start, left, right, end = 0, 0, len(A), len(A)
            while left < right:
                if A[left] == 1:
                    left += 1
                elif A[right-1] == 1:
                    right -= 1
                elif A[left] == 0:
                    A[start], A[left] = A[left], A[start]
                    left += 1
                    start += 1
                elif A[right-1] == 2:
                    A[right-1], A[end-1] = A[end-1], A[right-1]
                    right -= 1
                    end -= 1
                else:
                    A[left], A[right-1] = A[right-1], A[left]

Log in to reply
 

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