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.

Solution

计数排序

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
zeros = 0
ones = 0
twos = 0
for num in nums:
if num == 0:
zeros += 1
if num == 1:
ones += 1
if num == 2:
twos += 1

for i in range(zeros):
nums[i] = 0
for i in range(ones):
nums[zeros + i] = 1
for i in range(twos):
nums[zeros + ones + i] = 2

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void sortColors(int []nums){
int red=0,white=0,blue=0;
for(int i=0;i!=nums.length;i++){//first, count their numbers
if(nums[i]==0){
red++;
}else if(nums[i]==1){
white++;
}else{
blue++;
}
}
for(int i=0;i!=nums.length;i++){//second, rewrite the original array
if(i<red){
nums[i]=0;
}else if(i<white+red){
nums[i]=1;
}else{
nums[i]=2;
}
}
}