Python code, beats 99% submissions


  • 0

    0_1469167764406_Screen Shot 2016-07-21 at 12.31.55 AM.png

    class Solution(object):
        def swap(self, nums, i, j):
            t = nums[i]
            nums[i] = nums[j]
            nums[j] = t
            
        def sortColors(self, nums):
            n = len(nums); l = 0; r = n - 1; i = 0
            while l < r and i < r+1:
                while nums[i] == 2 and i<r+1:
                    self.swap(nums, i, r)
                    r -= 1
                if nums[i] == 0:
                    self.swap(nums, i, l)
                    l += 1
                i += 1
    

Log in to reply
 

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