0%

Leetcode 75 Sort Colors

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:

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

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 a one-pass algorithm using only constant space?

三指针

本问题被称为 荷兰国旗问题
,最初由 Edsger W. Dijkstra提出。
其主要思想是给每个数字设定一种颜色,并按照荷兰国旗颜色的顺序进行调整。

我们用三个指针(p0, p2 和curr)来分别追踪0的最右边界,2的最左边界和当前考虑的元素。

本解法的思路是沿着数组移动 curr 指针,若nums[curr] = 0,则将其与 nums[p0]互换;若 nums[curr] = 2 ,则与 nums[p2]互换。

算法

初始化0的最右边界:p0 = 0。在整个算法执行过程中 nums[idx < p0] = 0.

初始化2的最左边界 :p2 = n - 1。在整个算法执行过程中 nums[idx > p2] = 2.

初始化当前考虑的元素序号 :curr = 0.

While curr <= p2 :

若 nums[curr] = 0 :交换第 curr个 和 第p0个 元素,并将指针都向右移。

若 nums[curr] = 2 :交换第 curr个和第 p2个元素,并将 p2指针左移 。

若 nums[curr] = 1 :将指针curr右移。

算法参考于

作者:LeetCode
链接:https://leetcode-cn.com/problems/sort-colors/solution/yan-se-fen-lei-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
void sortColors(vector<int>& nums) {
int i = 0;
int j = 0;
int k = nums.size() - 1;
while(j <= k)
{
if(nums[j] == 1)
{
++j;
}
else if(nums[j] == 0)
{
swap(nums[j], nums[i]);
++i;
++j;
}
else if(nums[j] == 2)
{
swap(nums[j], nums[k]);
--k;
}
}
}
};