题目:

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?

代码:

class Solution {
public:
void sortColors(vector<int>& nums) {
int p0 = ;
int p2 = nums.size()-;
int i = ;
while ( i<=p2 )
{
if ( nums[i]== )
{
std::swap(nums[i++], nums[p0++]);
continue;
}
if ( nums[i]== )
{
std::swap(nums[i],nums[p2--]);
continue;
}
i++;
}
}
};

tips:

双指针技巧。

需要注意的是指针i什么时候移动。

1) 如果发现nums[i]==0,则swap之后i后移,p0后移。

2) 如果发现nums[i]==2,则swap之后i不动,p2前移。

3) 终止条件是i>p2

要保证指针i始终处于p0和p2之间:

因为1)的交换后,肯定保证nums[i]==1。

但是2)的交换后,不一定保证nums[i]是什么,因此i暂时不动。

但是,既然1)交换后肯定保证nums[i]==1,可不可以不直接nums[i++],而是把移动i的任务交给最后的i++呢?

这样做的不太方便的,因为如果首个元素就是0,就会导致i<p0了,所以最好这样写。

=============================================

第二次过这道题,核心在于记住“因为1)的交换后,肯定保证nums[i]==1。”

class Solution {
public:
void sortColors(vector<int>& nums) {
int zero = ;
int two = nums.size()-;
int i = ;
while ( i<=two )
{
if ( nums[i]== )
{
swap(nums[i++], nums[zero++]);
}
else if ( nums[i]== )
{
swap(nums[i], nums[two--]);
}
else
{
i++;
}
}
}
};

【Sort Colors】cpp的更多相关文章

  1. leetcode 【 Sort Colors 】python 实现

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

  2. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  3. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  4. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  5. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  6. POJ 1018 【枚举+剪枝】.cpp

    题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...

  7. 【Merge Intervals】cpp

    题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...

  8. 【Interleaving String】cpp

    题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...

  9. 【Combination Sum 】cpp

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

随机推荐

  1. .NET Framework 中的字符编码

    字符是可用多种不同方式表示的抽象实体. 字符编码是一种为受支持字符集中的每个字符进行配对的系统,配对时使用的是表示该字符的某些值. 例如,摩尔斯电码是一种为罗马字母表中的每个字符进行配对的字符编码,配 ...

  2. 在PeopleSoft系统中实现打印页面的功能

    我们知道,在PeopleSoft HCM里,一般上了薪酬模块的话,都会客户化工资单页面,去匹配公司之前的工资单的报表的格式.有的时候,这个工资单页面又需要打印出来,以供员工的使用.PeopleSoft ...

  3. PHP字符串拼接与MySQL语句

    这个部分总是钻牛角尖.总是出错. public function getList($pagesize=25){ $where = '1'; $tableName = $this->getTabl ...

  4. Win7更改默认打开方式失败

    问题描述:选定某个给定文件,然后鼠标右键选择打开方式,在浏览后选到自己期望使用的应用程序,然后单击确定后,却发现没有任何效果.原文件仍然保持原来的打开方式. 问题原因:更好程序或者升级程序时安装路径发 ...

  5. U6会计科目导入致对账不平

    一个客户,用的是U6版本,想将己使用的账套的科目导入新建的账套中,本想用用友本身自带的总账工具导入,发现导不了.没办法,只能打开数据库,手工导入. 月末结账时,发现对账不平.问题是余额表与明细不平,大 ...

  6. mysql 汉字乱码

    原因:mysql server character设置问题 一.检查mysql server 安装目录下my.ini文件 找到如下设置 [mysql] default-character-set = ...

  7. instanceof、==号、Objetc类

    1)instanceof: 判断某个对象是否为某个类的实例,注意多态的运用,一个父类引用指向子类的对象,根据继承,子类就是父类,所以子类也可以看做是父类的一个实例.  形式:引用 instanceof ...

  8. 在C#使用文件监控对象FileSystemWatcher的几种方案

    最近在项目中有这么个需求,就是得去实时获取某个在无规律改变的文本文件中的内容.首先想到的是用程序定期去访问这个文件,因为对实时性要求很高,间隔不能超过1S,而且每次获取到文本内容都要去分发给web服务 ...

  9. jquery-弹窗:layer

    键: 值 描述 下表的属性都是默认值,您可在调用时按需重新配置,他们可帮助你实现各式各样的风格.如是调用: $.layer({键: 值, 键: 值, …}); type: 0 层的类型.0:信息框(默 ...

  10. 管理员必备的20个Linux系统监控工具

    需要监控Linux服务器系统性能吗?尝试下面这些系统内置或附件的工具吧.大多数Linux发行版本都装备了大量的监控工具.这些工具提供了能用作取得相关信息和系统活动的量度指标.你能使用这些工具发现造成性 ...