天天看點

74 leetcode - Sort Colors

#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, 
with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You are not suppose to use the library's sort function for this problem.
'''
class Solution(object):
    def sortColors(self, nums):
        """未考慮輸入不合法
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        num0 = num1 =   #分别0,1的個數
        for i in nums:
            if i == :
                num0 += 
            elif i == :
                num1 += 

        for i in range(num0):
            nums[i] = 

        for i in range(num0,num0 + num1):
            nums[i] = 

        for i in range(num0 + num1,len(nums)):
            nums[i] = 

if __name__ == "__main__":
    s = Solution()
    a =  [,,,,,,,,,]
    s.sortColors(a)
    print a