[LeetCode] 桶排序的特殊解,例 Sort Color

时间:2022-06-01 20:31:41

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 {
public:
void sortColors(int A[], int n) {
}
};

像这种value被限定在一定范围内的排序,完全可以用桶排序做。

不过题目又加了限定,不让用桶排序。或者说,只允许单次遍历。

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?

题目的特点在于:取值个数非常少只有三个:0,1,2。这种情况下我们可以定义三个指针:i, j, k,分别指向0段, 1段, 2段的末尾。当然,初识时i,j,k是重合的,赋值为-1。

k先走,不管遇到什么值,都将该值存为tmp,然后将当前位置赋值为2。因为就算tmp不是2,比如是0,k也要为这个0挪地方,所以把k位置置为2没有错。接着如果tmp <= 1,j前进并将j位赋值为1,道理一样,也是为了腾地方。最后处理i。

之所以必须k先走,其次j,最后i,第一是因为排序要求必须 0,1,2;第二,考虑这种情况:当前i, j , k重合,k前进一步遇到的是0。因为k先走,所以虽然k位被先置为2了,后面的j i 会将这个位置的值覆盖成为正确的值。

class Solution {
public:
void sortColors(int A[], int n) {
if(n == ) return;
int i = -, j = -, k = -, tmp = ;
while(k < n-){
tmp = A[++k];
A[k] = ;
if(tmp <= ) A[++j] = ;
if(tmp == ) A[++i] = ;
}
return;
}
};

小结

固定取值范围内的排序可以用桶排序,如果取值范围很小,只有几个值。我们可以直接定义等量的指针,通过这种方式一次遍历解决问题。