【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 ...
随机推荐
- 自动计算尺寸列表功能案例ios源码
源码HTKDynamicResizingCell,HTKDynamicResizingCell提供自动计算尺寸的TableViewCell/CollectionViewCel,只要设置了合适AutoL ...
- C#操作xml
最进在做一个项目,需要用到xml做配置文件,但是自己忘的差不多了,所以就温习了一遍.以下是我项目中所用到的,所以肯定也不全面. 1.新建xml文件 using System.Xml;//需要引用 st ...
- SQL SERVER中查询参数为空(null)时默认查询所有的实现
最近在项目中碰到一个比较有意思的问题,网上查找了一些方法,在这里总结分享一下. 我们经常会碰到这样的场景:需要查询数据,有一些查询条件,但是查询的时候,我们希望在某个条件为空的时候,则不筛选这个条件, ...
- spring 知识梳理
https://github.com/spring-projects/spring-framework spring github地址
- iOS中使用子线程的完整方法
http://www.cnblogs.com/ygm900/archive/2013/06/23/3151691.html 第一步:开启子线程 //开启子线程到网络上获取数据 myFirstThrea ...
- 【标题】一本帮你提高Excel办公效率的VBA书
公司工程部男同事,EXCEL能力最强的前三位,分别号称:大表哥 二表哥 三表哥 公司财务部女同事,EXCEL能力最强的前三位,分别号称:大表姐 二表姐 三表姐 想成为你们公司的“表哥”.“表姐”? 但 ...
- LINUX开启允许对外访问的网络端口
LINUX开启允许对外访问的网络端口 LINUX通过下面的命令可以开启允许对外访问的网络端口: /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCE ...
- 使用Telerik控件搭建Doubanfm频道部分
今天感觉好累啊..还是坚持记录下. 收集的API: https://github.com/HakurouKen/douban.fm-api https://github.com/zonyitoo/do ...
- 菜鸟学习Hibernate——多对多关系映射
Hibernate中的关系映射,最常见的关系映射之一就是多对多关系映射例如用户与角色的关系,一个用户对应多个角色,一个角色对应多个用户.如图: Hibernate中如何来映射这两个的关系呢? 下面就为 ...
- eclipse java 空心J文件的回复
eclipse中的空心J的java文件,表示当前文件不包含在项目中进行编译,而仅仅是当做资源存在项目中. 解决方案如下: 1.鼠标右击当前空心j文件,-->build path-->inc ...