题目:

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. JDK+Eclipse+MyEclipse+tomcat的安装与配置

    以下我所使用的各软件版本为:JDK(1.6):eclipse(3.2.2):myEclipse(5.5.1GA):tomcat(5.5.12): 一.安装JDK: 下载完JDK(1.6)后双击进行安装 ...

  2. C# ITextShap 生成PDF 下载

    using iTextSharp.text; using iTextSharp.text.pdf; //创建 Document Document pdfDoc = new Document(new R ...

  3. 微信分享jssdk config:invalid signature 签名错误

    使用微信分享时,按照官方给的demo,使用时一直提示签名错误. 根据微信开发文档(http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd ...

  4. juqery easy ui 实现二级菜单联动

    实现效果 代码: <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat= ...

  5. memcached在Windows下的安装

    memcached简介详情请谷歌.这里介绍如何在windows下安装. 1.下载     下载地址:http://download.csdn.net/detail/u010562988/9456109 ...

  6. [TYVJ] P1017 冗余关系

    冗余关系 背景 Background 太原成成中学第3次模拟赛 第4题   描述 Description Mrs.Chen是一个很认真很称职的语文老师 ......所以,当她看到学生作文里的人物关系描 ...

  7. shell 脚本监控程序是否正在执行, 如果没有执行, 则自动启动该进程

    代码里面监控1个进程, 代码很简单, 我就不讲解了, 有不懂的, 可以在回复里面问. 我看见了会给予讲解. 当然了, 该脚本要执行,你需要开启系统的定时器进程 crond , 并且编辑配置文件. 执行 ...

  8. Send竞争对手:百度云一小时,QQ超大附件最多支持2G,邮件附件20M到50M不等(附国外所有storage列表)——痛点是,最大传输2G,最大容量只有3G(和微云不是一回事),转存到微云文件不能超过1G

    QQ邮箱最大可发送50M普通附件(群邮件则限制在2M).此外也可以使用超大附件功能,支持将1G的文件发往任意邮箱.QQ邮箱根据你的QQ邮箱容量的不同制定相应的接受附件限制,包括附件在内,2G用户所发送 ...

  9. 详解JavaScript中的Object对象

    Object是在javascript中一个被我们经常使用的类型,而且JS中的所有对象都是继承自Object对象的.虽说我们平时只是简单地使用了Object对象来存储数据,并没有使用到太多其他功能,但是 ...

  10. Python Cookie HTTP获取cookie并处理

    Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...