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?

Hide Tags

Array Two Pointers Sort

 

 
  这题还是挺简单的,就是对只有3个类型(0 1 2)的排序,题目中给了一个两次遍历的排序方法,即统计出现的次数然后填回数组,要求是遍历一次完成,看了一下discuss ,实现逻辑类似,即写的如何。
 
思路:
  1. 创建一个zeroidx 表示0 的末尾下标,初始化为0,twoidx 表示2的下标,初始化为n,不同地方是前者操作时++,后者--。
  2. 遍历数组如果遇到1,则继续遍历。
  3. 如果遇到0,则替换 zeroidx 与i 上的值,然后zerosidx +1,如果此时该位置上的值位0,则数组从0 to i 上为0,所以zeroidx = i+1.
  4. 如果遇到2,则 twoidx -1, 然后替换 twoidx 与i 的值,因为替换后的i 位置上的值未判断,所以i-1 进行多一次该位置上的遍历。

3.中zeroidx 可以一直+1 不直接跳位,这样需要多次交换,discuss 实现多是这样。

下面是我写的代码:

 #include <iostream>
using namespace std; class Solution {
public:
void sortColors(int A[], int n) {
int zeroIdx = ;
int twoIdx = n;
for(int i =;i<n&&i<twoIdx;i++){
if(A[i]==){
continue;
}
if(A[i]==){
A[i] = A[zeroIdx];
A[zeroIdx] = ;
if(++zeroIdx<n&&A[zeroIdx]==) zeroIdx=i+;
}
else if(A[i]==){
twoIdx--;
A[i]=A[twoIdx];
A[twoIdx]=;
i--;
}
else
return ;
}
return ;
}
}; int main()
{
int a[] = {,,,};
Solution sol;
sol.sortColors(a,sizeof(a)/sizeof(int));
for(int i=;i<sizeof(a)/sizeof(int);i++)
cout<<a[i]<<" ";
cout<<endl;
return ;
}

discuss 中有一个实现非常不错,逻辑清晰,可以将变量数3 扩展为k 个,只要不怕写起来麻烦,其逻辑类似于插入排序,将遍历的项插入正确的位置:

  1. 为3个(k个) 变量创建index a b c= -1;
  2. 遍历数组,如果为0,顺序修改a[++c] a[++b] a[++a],这样如果abc 一样,最终只有修改了 a[++a] 这一项,然后同时又更新了3者的idx。
  3. 如果为1,则顺序修改 a[++c] a[++b],这样 0的index 未变。
  4. 如果为2,则顺序修改 a[++c]。
  public void sortColors(int[] A) {

     int i=-, j=-, k=-;

     for(int p = ; p < A.length; p++)
{
if(A[p] == )
{
A[++k]=;
A[++j]=;
A[++i]=;
}
else if (A[p] == )
{
A[++k]=;
A[++j]=; }
else if (A[p] == )
{
A[++k]=;
}
} }
 
 
 
 
 
 
 
 
 

[LeetCode] Sort Colors 只有3个类型的排序的更多相关文章

  1. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  2. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. [leetcode]Sort Colors @ Python

    原题地址:https://oj.leetcode.com/problems/sort-colors/ 题意: Given an array with n objects colored red, wh ...

  4. [Leetcode] Sort Colors (C++)

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  5. 75.[LeetCode] Sort Colors

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  6. [LeetCode] Sort Colors 对于元素取值有限的数组,只遍历一遍的排序方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  7. LeetCode Sort Colors (技巧)

    题意: 一个数组只可能含有3种数据,分别为1,2,3,请将数组排序(只能扫一遍). 思路: 如果扫两遍的话,用个桶记录一下,再赋值上去就行了. class Solution { public: voi ...

  8. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  9. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

随机推荐

  1. jdk配置与环境变量配置

    1.1.下载jdk1.8.0,如图所示 解压放在你用的位置 在官网中下载 1.2.配置环境变量 打开环境变量:计算机-->属性-->高级系统配置-->环境变量 配置JAVA_HOME ...

  2. Linux curl命令中,HTTP 302处理

    在Linux中使用curl命令时,偶尔会遇到一些URL跳转到新的URL,即HTTP中的3XX(redirection,重定向 ). $curl -s -I $URL > log 这时在返回的报文 ...

  3. mac 升级EI Capitan后遇到c++转lua时遇到libclang.dylib找不到的错

    升级EI Capitan后,打包lua脚本时,会报这个错: LibclangError: dlopen(libclang.dylib, 6): image not found. To provide ...

  4. vs2005无法附加到进程 系统找不到文件

    用管理员身份打开vs2005. 注意配置.

  5. 【线段树 扫描线 二维数点】loj#6276. 果树

    路径计数转成二维数点很妙啊 题目描述 NiroBC 姐姐是个活泼的少女,她十分喜欢爬树,而她家门口正好有一棵果树,正好满足了她爬树的需求. 这颗果树有 $N$ 个节点,标号 $1 \ldots N$ ...

  6. OOP之单例模式

  7. python中打印金字塔和九九乘法表的几种方法

    # 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...

  8. poj 1862 2*根号(n1*n2)问题 贪心算法

    题意: 有n个数,要把其中2个数进行2*根号(n1*n2)操作,求剩下最小的那个数是多少? 哭诉:看题目根本没看出来要让我做这个操作. 思路: 每次把最大的,次大的拿出来进行操作 用"优先队 ...

  9. ubuntu12.04ppa安装emacs24

    ppa地址:https://launchpad.net/~cassou/+archive/emacs 因为debian版本的emacs-snapshot维护者停止更新,所有ubuntu上的也停止了. ...

  10. Python虚拟机之异常控制流(五)

    Python中的异常控制语义结构 在Python虚拟机之异常控制流(四)这一章中,我们考察了Python的异常在虚拟机中的级别上是什么东西,抛出异常这个动作在虚拟机的级别上对应的行为,最后,我们还剖析 ...