Python simple solution using movezeros trick


  • 0
    A

    Using another quesition in LC 'move zeros' but just do it twice.

    class Solution(object):
        def sortColors(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            
            self.movezeros(nums,1)
            self.movezeros(nums,2)
            
        def movezeros(self,nums,val):
            
            vals = 0
            for i in range(len(nums)):
                if nums[i]!=val:
                    nums[i-vals],nums[i] = nums[i],nums[i-vals]
                else:
                    vals+=1
        
    

Log in to reply
 

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