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.

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.

click to show follow up.

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的个数,就可以计算出各自的下标,将0,1,2放入对应的位置。
C++实现代码如下:
#include<iostream>
using namespace std;

class Solution {
public:
    void sortColors(int A[], int n) {
        int count0,count1,count2;
        count0=count1=count2=0;
        int i;
        for(i=0;i<n;i++)
        {
            if(A[i]==0)
                count0++;
            else if(A[i]==1)
                count1++;
            else
                count2++;
        }
        i=0;
        int j=count0;
        int k=count0+count1;
        while(i<count0)
        {
            if(A[i]==0)
                i++;
            else if(A[i]==1)
            {
                swap(&A[i],&A[j]);
                j++;
            }
            else if(A[i]==2)
            {
                swap(&A[i],&A[k]);
                k++;
            }
        }
        while(j<count0+count1)
        {
            if(A[j]==1)
                j++;
            else if(A[j]==2)
            {
                swap(&A[j],&A[k]);
                k++;
            }
        }
    }
    void swap(int *a,int *b)
    {
        int temp=*a;
        *a=*b;
        *b=temp;
    }
};

int main()
{
    Solution s;
    int arr[10]={0,1,2,2,1,1,0,2,0,2};
    s.sortColors(arr,10);
    for(auto a:arr)
        cout<<a<<" ";
    cout<<endl;
}

方法二:使用快速排序的分割方法。使用i,j,k三个下标,i始终指向的是第一个1,而j始终执行的是最后一个2的前一个位置。k用来遍历整个数组。

C++实现代码:

#include<iostream>
using namespace std;

class Solution {
public:
    void sortColors(int A[], int n) {
        if(n==0)
            return;
        int i=0;
        int k=0;
        int j=n-1;
        while(k<=j)
        {
            if(A[k]==0)
            {
                swap(&A[k],&A[i]);
                k++;
                i++;
                continue;
            }
            if(A[k]==2)
            {
                swap(&A[k],&A[j]);
                j--;
                continue;
            }
            k++;
        }
    }
    void swap(int *a,int *b)
    {
        int temp=*a;
        *a=*b;
        *b=temp;
    }
};

int main()
{
    Solution s;
    int arr[11]={0,1,2,2,1,1,0,2,0,2,2};
    s.sortColors(arr,1);
    for(auto a:arr)
        cout<<a<<" ";
    cout<<endl;
}

 

相关文章
|
索引
LeetCode 75. Sort Colors
给定一个具有红色,白色或蓝色的n个对象的数组,对它们进行就地排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色。 这里,我们将使用整数0,1和2分别表示红色,白色和蓝色。 注意:您不应该使用库的排序功能来解决此问题。
31 0
LeetCode 75. Sort Colors
|
索引
LeetCode 81. Search in Rotated Sorted Array II
假设按升序排序的数组在事先未知的某个枢轴处旋转。 (例如, [0,0,1,2,2,5,6] 可能变成 [2,5,6,0,0,1,2]). 给定一个要搜索的目标值T, 如果该目标值能在数组中找到则返回true,否则返回false。
62 0
LeetCode 81. Search in Rotated Sorted Array II
|
JavaScript Windows 移动开发
[LeetCode]--75. 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
1188 0
LeetCode 75 Sort Colors(颜色排序)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50776242 翻译 给定一个包含红色、白色、蓝色这三个颜色对象的数组,对它们进行排序以使相同的颜色变成相邻的,其顺序是红色、白色、蓝色。
740 0
LeetCode - 33. Search in Rotated Sorted Array
33. Search in Rotated Sorted Array  Problem's Link  ---------------------------------------------------------------------------- ...
724 0
|
C++
[LeetCode] Search in Rotated Sorted Array II
For those who have already solved Search in Rotated Sorted Array, this problem can be solved similarly using codes for that problem and simply adding codes to skip the duplicates.
858 0