题目:

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?

Tag:
Array, Two Pointers, Sort
体会:
这个题是看了别人的做法才后知后觉的。这个题可以说是quicksort里面的那个partition的又一个变体:partition成多段。原来的partition是只分成两段,x <= pivot 和 x > pivot。
而这道题是要求分成三段,假设我们在某个中间过程,我们正要来检查A[p] 上的元素,此时的A应该是这样子的:
    [0,0,..0,1,1,..1,2,2,..2, p, unsorted part]
        i    j        p-1
我们需要有两个指针,一个指针指着0部分的最后一位的index, 一个指着1部分的最后一位的index, 2部分的最后一位我们可以免费获得,即p-1。然后我们就来看A[p],如果A[p]是2,什么也不做就可以继续维持原先的循环不变式;如果A[p]是1的话,那么1部分的size会变大一位,所以要j = j + 1,然后把A[j]置成1,把A[p]置成2;类似地,如果是A[p] = 0,那么0部分要变大,i = i+1, A[i] = 0, 1部分跟着顺延,j = j+1, A[j] = 1, 最后A[p]=2。
在原来的partition算法中本来是应该指针变大了以后进行“交换”操作的,而这道题不用交换可以直接赋值成0,1,或者2,是因为我们知道要赋的值。举个例子
partition中:
  [0, 6, 5, 10, 12, 13, p,  .... pivot=9]
      i   
 当检查到p位,如果p<=pivot,那么是i = i +1, A[i] <-> A[p], (即A[i] 和A[p]进行交换)。
但是这道题,我们知道如果是0部分的i = i + 1, 那么A[i] 一定是要变成0, 1部分扩大的话,j = j+1, A[j] 一定是要变成1,所以可以直接赋值,而不用交换。
啊啊,我说的太啰嗦了,完全是为了给自己做个笔记,大家不要拍我啊。。。

 class Solution {
public:
void sortColors(int A[], int n) {
int i = -;
int j = -;
for (int p = ; p < n; p++) {
if (A[p] == ) {
// do nothing but moving the index of k
} else if (A[p] == ) {
//
A[p] = ;
A[++j] = ;
} else {
A[p] = ;
A[++j] = ;
A[++i] = ;
}
}
}
};


[Leetcode] Sort Colors (C++)的更多相关文章

  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. 75.[LeetCode] Sort Colors

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

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

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

  6. [LeetCode] Sort Colors 只有3个类型的排序

    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. Windows单击右键没有共享选项怎么办

    文件共享是指在网络环境下文件.文件夹.某个硬盘分区使用时的一种设置属性,一般指多个用户可以同时打开或使用同一个文件或数据.但有时候也会遇到找不到共享选项的情况. Windows单击右键没有共享选项怎么 ...

  2. ubuntu没有进入图形界面解决办法

    可以通过设置runlevel 为2 来控制以后的登陆,或者是升级不完全.中间出错了,无法正常登陆.有2种方式来进入图形界面: 1. 登陆系统后,输入如下命令来启动图形界面: startx 2. 登陆系 ...

  3. Python Set集合,函数,深入拷贝,浅入拷贝,文件处理

    1.Set基本数据类型 a.set集合,是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set o ...

  4. [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  5. 转载收藏之用 - 微信公众平台开发教程(五):使用Senparc.Weixin.MP SDK

    Senparc.Weixin.MP SDK已经涵盖了微信5.0的所有公共API,以及2013年10月29日升级之后大部分实用的接口. 整个项目的源代码以及已经编译好的程序集可以在这个项目中获取到:ht ...

  6. Keil "RECURSIVE CALL TO SEGMENT"彻底解决

    我们在做菜单程序或通过函数指针调用函数时,如果被调用的函数中有包含了常量字符串,那么经常会出现这样的的错误提示:"RECURSIVE CALL TO SEGMENT"意思是&quo ...

  7. QDialog 添加最大化、最小化按钮和关闭按钮,并且要正常显示

    在使用QDialog时,默认情况下只有“这是什么”和“关闭”按钮(不知道为什么QT要这么做),但是我们习惯有最大化和最小化按钮.本文介绍如何在该模式下如何设置. 新建一个QDialog工程,然后打开D ...

  8. 【转】linux tree命令以树形结构显示文件目录结构 ---- 不错

    原文网址:http://jingyan.baidu.com/article/acf728fd19c7eff8e510a3eb.html 今天小编来给分享Linux 系统下一个非常有用的命令的使用:tr ...

  9. Asp.net MVC中的ViewData与ViewBag(转)

    在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...

  10. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...