hash依次統(tǒng)計red white blue的次數(shù)形帮,
然后根據(jù)次數(shù)依次對nums[i] 賦值
要注意每次賦值時候的起始坐標
void sortColors(int* nums, int numsSize) {
int red = 0, white = 1, blue = 2;
int hash[3] = {0};
for(int i = 0; i < numsSize; i++)
switch(nums[i]){
case 0:
hash[red]++;break;
case 1:
hash[white]++;break;
case 2:
hash[blue]++;break;
default:
break;
}
for(int i = 0; i < hash[red]; i++)
nums[i] = red;
for(int i = 0; i < hash[white]; i++)
nums[i+hash[red]] = white;
for(int i = 0; i < hash[blue]; i++)
nums[i+hash[white]+hash[red]] = blue;
}