Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]

My code:

class Solution:
    def sortColors(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        tmp_dict={}
        for item in nums:
            if item not in tmp_dict.keys():
                tmp_dict[item]=1
            else:
                tmp_dict[item]+=1
        count=0
        for item in tmp_dict.keys():
            value=tmp_dict[item]
            for eid in range(count,count+value):
                nums[eid]=item
            count+=value
        #return nums
        

Solution: https://leetcode.com/problems/sort-colors/