Python version based on Dutch National Flag problem


  • 2
    G
    class Solution:
        # @param A a list of integers
        # @return nothing, sort in place
        # 11:37
        def sortColors(self, A):
            i, j, n = 0, 0, len(A) - 1
            
            while j <= n:
                if A[j] < 1:
                    A[i], A[j] = A[j], A[i]
                    i += 1
                    j += 1
                elif A[j] > 1:
                    A[j], A[n] = A[n], A[j]
                    n -= 1
                else:
                    j += 1

Log in to reply
 

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