题目:

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. jQuery插件学习(一)

    由于项目开发需要,经常会用到一些jquery插件,但网上已有的插件常常又不能100%满足业务需求,所以就想自己能看懂插件的代码,进行一些功能上的改动和补充,或者能自己自定义插件就更好了.所以这段时间会 ...

  2. linux运维基础__争取十月前研究的差不多

    转来的一编,考虑在十月前研究的差不多 linux运维人员基础 1.很多地方经常会用到的rsync工具 实施几台服务器的同步效果 我们公司就是使用这个工具完成服务器的游戏的服务端和客户端同步,有几个文章 ...

  3. table行转列

    table行转列 摘要 在使用ews调用exhange的收件箱的并在h5页面显示邮件详情的时候,因为返回的每封邮件的内容都是htmlbody,没有textbody.每封邮件又没什么规律,用正则表达式来 ...

  4. Effective Java2读书笔记-类和接口(三)

    第17条:要么为继承而设计,并提供文档说明,要么就禁止继承 第18条:接口优于抽象类 这两条中,提到了一个很重要的概念骨架实现.也就是说,抽象类实现接口的形式.这样的好处是,接口本来不能提供默认的实现 ...

  5. Ubuntu下配置NFS服务

    Table of Contents 1.下载相关软件 2.建立共享目录 3.修改该配置文件 4.重启服务 5.测试服务器 6.测试客户端 测试系统:Ubuntu8.04 1.下载相关软件 使用如下命令 ...

  6. 【CF 189A Cut Ribbon】dp

    题目链接:http://codeforces.com/problemset/problem/189/A 题意:一个长度为n的纸带,允许切割若干次,每次切下的长度只能是{a, b, c}之一.问最多能切 ...

  7. Java异步调用Future对象

    Future类存在于JDK的concurrent包中,主要用途是接收Java的异步线程计算返回的结果. 个人理解的使用场景大概如下: 有两个任务A和B,A任务中仅仅需要使用B任务计算成果,有两种方法实 ...

  8. OpenstackHigh-level-service

    1,yum -y install openstack-cinder;

  9. JUnit三分钟教程 ---- 快速起步

    JUnit三分钟教程 ---- 快速起步 摘自http://lavasoft.blog.51cto.com/62575/65625/ JUnit是个好东西,做大点的项目离不开这东西,实际中用的时候也因 ...

  10. 怎样实现IOS开发中的数据存储方式

    iOS 开发中,一般有如下几种数据存储方式.需要根据具体的业务场景,选择 合适的数据存储方式. (1)  用户默认设置 – 这种情况通常不需要用户干预,如游戏通关信息,Video 播放记录,或者 Ap ...