【Sort Colors】cpp
题目:
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.
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的更多相关文章
- leetcode 【 Sort Colors 】python 实现
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 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~ ...
- POJ 1018 【枚举+剪枝】.cpp
题意: 给出n个工厂的产品参数带宽b和价格p,在这n个工厂里分别选1件产品共n件,使B/P最小,其中B表示n件产品中最小的b值,P表示n件产品p值的和. 输入 iCase n 表示iCase个样例n个 ...
- 【Merge Intervals】cpp
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
随机推荐
- [原]My first Python
我的第一个Python程序: print 'hello world' raw_input ("print any key to continue...") 在python3.4下应 ...
- requireJS心得
最近有幸接触到前端分模块加载JS框架,并且结合avalonJS使用,在此记录学习痕迹: a.实现js文件的异步加载,避免网页失去响应: b.管理模块之间的依赖性,便于代码的编写和维护. (1)requ ...
- 第 2章 数组和 ArrayLists
数组是最通用的数据结构,它出现在几乎所有的编程语言里.在 C#语言中使用数组包括创建 System.Array 类型的数组对象,以及创建针对所有数组的抽象的基类型.Array 类提供了一套方法,这些方 ...
- C#虚方法认识
C# 中虚方法 1.具体的是new后面具体是那个类,调用的是该类的实现方法.不和基类有关,虽然可以将子类赋值给基类句柄. 但是具体还是调用具体实例化的方法.
- Linux命令学习---目录
一.文件相关命令 1.文件显示 1)tail,head,more,less,cat,nl
- LevelDB源码之四LOG文件
“LOG文件在LevelDb中的主要作用是系统故障恢复时,能够保证不会丢失数据.因为在将记录写入内存的Memtable之前,会先写入Log文件,这样即使系统发生故障,Memtable中的数据没有来得及 ...
- android空鼠修改
抛弃盒子自带遥控器后,又不满意改键红外遥控器,选择飞鼠及无线键鼠成为最终方案.问题是:菜单键如何实现!其实很简单:即插即用USB无线飞鼠及键鼠套装只涉及2个文件:system/usr/layout/G ...
- BufferedReader,缓冲输入字符流
1. /** * 输入字符流: * --------|Reader 所有输入字符流的基类.抽象类 * -----------|FileReader 读取文件字符串的输入字符流. * --------- ...
- 【转】DCC32的参数详解
完整的内容如下: // DCC32编译器的设置说明. // Dcc32 [options] filename [options] // DCC32 [操作选项] 文件名称 [操作选项] // -A&l ...
- 代码重启SQL命令
1.net stop mssqlserver --停止mssqlserver 2.net start mssqlserver --开始mssqlserver