题目
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.
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?
思路一
两遍扫描,第一遍从前往后将所有的 2 放到尾部,第二遍从后往前将所有的 0 放到头部。
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<=0)
return ;
int i=0;
int j=n-1;
while(i<=j)
{
if(A[i]<2)
i++;
else
{
if(A[j]!=2)
{
A[i] = A[j];
A[j] = 2;
j--;
}
else
j--;
}
}
i=0;
while(i<=j)
{
if(A[j]>0)
j--;
else
{
if(A[i]!=0)
{
A[j] = A[i];
A[i] = 0;
i++;
}
else
i++;
}
}
}
};
思路二
快速排序的 Partition 操作,也需要两遍
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<=0)
return ;
int i=0;
int j=n-1;
while(i<j)
{
while(i<n && A[i]==0) i++;
while(j>=0 && A[j]==2) j--;
if(i>=j)
break;
if(A[i]!=A[j])
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
else
i++;
}
i=0;
while(i<j)
{
while(i<n && A[i]==0) i++;
while(j>=0 && A[j]==2) j--;
if(i>=j)
break;
if(A[i]!=A[j])
{
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
else
j--;
}
}
};
思路三
p0 指向要插入 0 的位置,p2指向要插入 2 的位置。
class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(n<=0)
return ;
int p0 = 0;
int p2 = n-1;
for(int i=0;i<n && p0<p2;i++)
{
if(A[i]==0)
{
A[i] = A[p0];
A[p0++] = 0;
}
if(A[i]==2)
{
A[i] = A[p2];
A[p2--] = 2;
i--;
}
}
return ;
}
};
最新 java
public class Solution {
public void sortColors(int[] nums) {
int zeroPosition = 0;
int onePosition = 0;
int twoPosition = 0;
for(int i = 0; i<nums.length; i++){
if (nums[i] == 0){
nums[twoPosition] = 2;
nums[onePosition] = 1;
nums[zeroPosition] = 0;
zeroPosition++;
onePosition++;
twoPosition++;
}
else if (nums[i] == 1){
nums[twoPosition] = 2;
nums[onePosition] = 1;
onePosition++;
twoPosition++;
}
else if (nums[i] == 2){
nums[twoPosition] = 2;
twoPosition++;
}
}
}
}
引用至: http://www.lifeincode.net/programming/leetcode-sort-colors-java/
Maybe we could use some sorting algorithm, but since there are only 3 possible values, we have a better approach. As mentioned by the problem, we can 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.
But if we want to do it in one-loop? We can see if we have a subarray, which is sorted, and there is a new element needed to be inserted, we can set the border of the different values and insert the new value, which means that we do not need to move all elements behind this new element to give it a position. For example, during sorting, if the array is like the following:
| a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | a[7] | a[8] | … |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 2 | 2 | 2 | 1 | 2 | 0 | … |
Now sub-array a[0] – a[5] is sorted. We want to add new element a[6], which is 1. The border of 0 is 2. The border of 1 is 3. The border of 2 is 6. Now we need to move all 2s one position. In fact we can only do it by setting a[2’s border]=2. Now we have a new position, which is a[1’s border], so we can let this position equals to 1. After the operation, the array becomes:
| a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | a[6] | a[7] | a[8] | … |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 2 | 2 | 2 | 2 | 0 | … |
We can continue doing this until we meet the end of the array.
344

被折叠的 条评论
为什么被折叠?



