leetCode(30):Sort Colors
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.
void sortColors(vector<int>& nums)
{//排序时间复杂度O(n)
int length=nums.size();
int zero=0;
int two=length-1;
while(zero<length && nums[zero]==0)
{//找到第一个不为0的数的下标
zero++;
}
while(two>=zero && nums[two]==2)
{//找到倒数第一个不为2的数的下标,zero前的数都是0
two--;
} for(int i=zero;i<=two;++i)
{
if(nums[i]==0)
{//假设为0,和zero指向的数交换,zero前移
nums[i]=1;//zero指向的值肯定是1,首先不可能为0,其次,i遍历过的地方,2都已经换到最后
nums[zero]=0;
zero++;
}
else if(nums[i]==1)
{//假设为1,则跳过
continue;
}
else
{//假设为2。则和two指向的数交换,并更新two的值。
//和zero不同,two前面的数可能还未排序
nums[i]=nums[two];
nums[two]=2;
two--;
while(two>=i && nums[two]==2)
{
two--;
}
i--;//先向后退一步。由于two指向的数还未进行排序
}
}
}
leetCode(30):Sort Colors的更多相关文章
- [Leetcode Week2]Sort Colors
Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [LeetCode题解]: Sort Colors
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an a ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【leetcode】Sort Colors(middle)☆
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- RabbitMQ安装后,BADARG问题
最近RabbitMQ安装后始终不能运行,发现异常关键信息如下 =CRASH REPORT==== 10-Nov-2017::13:41:09 === crasher: initial call: ap ...
- js数据管理的思考
最近要做一个农场项目,涉及到很多js数据管理的需求,这里也做下总结,不断的总结,再修正内容,也是快速进步的方法. 数据管理几个方面考虑: * 设置(更新)字段值 * 检索,根据id, index, 属 ...
- jQuery学习笔记之DOM操作、事件绑定(2)
jQuery学习笔记之DOM操作.事件绑定(2) --------------------学习目录------------------------ 4.DOM操作 5.事件绑定 源码地址: https ...
- overflow onclick ondblclick 练习
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【PostgreSQL-9.6.3】创建、修改、删除数据库
1.创建数据库 create database database_name; 2.修改数据库的名称 alter database database_name rename to new_databas ...
- 【Oracle】Rman备份策略
1. 查看可设置参数 RMAN> show all; RMAN configuration parameters for database with db_unique_name DRZ are ...
- 分割字符串 ExtractStrings
//分割字符串 ExtractStrings var s: String; List: TStringList; begin s := 'about: #delphi; #pascal, progra ...
- 使用Visio—UML画类图
在一个VS工程中,由于类的个数较多,而参数描述不是特别清晰.若此工程的生命周期较长,则有必要对工程进行完整分析,给出完整的文档.需要画出类图,并对每个成员进行详细描述. 一.画出类图 在VIsio中, ...
- 程序员客栈与DaoCloud这两家企业联手后,运维工程师要失业了!
2017年1月11日 ,程序员客栈与DaoCloud正式建立合作伙伴关系,为创业企业和团队提供容器应用解决方案.通过与DaoCloud合作,客栈可以更有效地把控开发环节中的不确定因素,解决项目工期不确 ...
- sql_3 join
http://www.cnblogs.com/rush/archive/2012/03/27/2420246.html