题目:

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. C/C++ 中##(两个井号)和#(一个井号)用法

    ##(两个井号)和#(一个井号)都是什么意思 连接符 ##(两个井号) 不知道什么符 #(一个井号) ## 连接符号由两个井号组成,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成 ...

  2. 如何让你的 footer 总是在浏览器底部无论什么分辨率无论什么浏览器?

    一个可以用的CSS驱动的可粘在底部的Footer 我们曾经都或多或少的试过用CSS来把Footer固定在底部的经理,但是他们总是不能正常的粘在底部,不是吗?可喜的是,痛苦的研究如何让footer粘在底 ...

  3. iOS学习之视图加载过程中会触发的方法(loadView/viewDidLoad/didReceiveMemoryWarning)

    1.loadView 这是视图控制器用来加载根视图的方法; 如果需要将自定义的视图作为根视图,则不需要调用父类对该方法的实现([super loadView]);直接将自定义视图通过self.view ...

  4. 爬虫框架YayCrawler

    爬虫框架YayCrawler 各位好!从今天起,我将用几个篇幅的文字向大家介绍一下我的一个开源作品——YayCrawler,其在GitHub上的网址是:https://github.com/liush ...

  5. Java学习笔记--多线程

    rollenholt的博文:http://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html 弹球例子: 0. 创建Bounce框架 ...

  6. ORA-19502: write error on file "", blockno (blocksize=)/linux下磁盘空间满了解决办法--Virtualbox

    今天,在测试环境启动数据库时,报错: SQL> startup; ORACLE instance started. Total System Global Area  285212672 byt ...

  7. 使用SALT-API进入集成开发的简单样例

    测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...

  8. BZOJ 1068 (区间DP)

    题意:字符串的压缩,f[l][r][0]代表还没M,f[l][r][1]代表有M. #include<cstdio> #include<cmath> #include<c ...

  9. LeetCode_Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  10. python 文件中的中文编码解决方法

    # -*- coding: utf-8 -*- #查看安装的SDK默认的编码字符集在脚本中可以修改你的编码格式, 方法如下:#sys.getdefaultencoding()#reload(sys)# ...