Sort colors系列
75. Sort Colors
问题描述:
给一个包含n个数字的数组,其中有0,1,2;排序使得所有相同的数字相邻,且按照0,1,2的顺序。
思路:
(1)计数排序:
需要扫两遍数组,一遍统计个数,第二遍开始摆放数字。
代码如下:
void sortColors(vector<int>& nums) {
int i=,j=,k=;
int n=nums.size();
for(int p=;p<n;p++)
{
if(nums[p]==)
i++;
else if(nums[p]==)
j++;
else
k++;
}
for(int p=;p<n;p++)
{
if(p<i)
nums[p]=;
else if(p>=i&& p<i+j)
nums[p]=;
else
nums[p]=;
}
}
(2)如果只能扫一遍,很容易想到的就是左边存放0和1,右边存放2.两边往中间靠。
设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.
然后使用i从头到尾扫一遍,直到与right相遇。
i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。
需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此i++;
而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断。
由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2
0 0 0 1 1 1 2 1 0 2 1 2 2 2
^ ^ ^
left i right
最直接的方法就是partition方法,代码如下:
void sortColors(int A[], int n) {
int left = ;
int right = n-;
int i = ;
while(i <= right)
{
if(A[i] == )
{
swap(A[left], A[i]);
left ++;
i ++;
}
else if(A[i] == )
{
i ++;
}
else
{
swap(A[i], A[right]);
right --;
}
}
}
(3)最漂亮的一种做法:
【最直观:平移插入】
void sortColors(int A[], int n) {
int i = -;
int j = -;
int k = -;
for(int p = ; p < n; p ++)
{
//根据第i个数字,挪动0~i-1串。
if(A[p] == )
{
A[++k] = ; //2往后挪
A[++j] = ; //1往后挪
A[++i] = ; //0往后挪
}
else if(A[p] == )
{
A[++k] = ;
A[++j] = ;
}
else
A[++k] = ;
}
}
Sort colors系列的更多相关文章
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- 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 ...
随机推荐
- 使用js将div高度设置为100%
在开发的工程中使用到了一些开源的bootstrap模板进行开发,在遇到一些需要替换的内容部分部分时,经常出现高度设置100%无法生效的问题,这里来用js强行设置一下. 思路:js监听窗口的缩放 ...
- JS数据结构与算法--双向链表
双向链表中链接是双向的:一个链向下一个元素,另一个链向上一个元素,如下图所示: 双向链表结构代码如下: class Node { constructor(element) { this.element ...
- Noip 训练指南
目录 Noip 训练指南 图论 数据结构 位运算 期望 题解 Noip 训练指南 目前完成 \(4 / 72\) 图论 [ ] 跳楼机 [ ] 墨墨的等式 [ ] 最优贸易 [ ] 泥泞的道路 [ ] ...
- JAVA基础篇—基本数据类型
Java8种基本数据类型:4个整型byte 1字节 8bit 范围-128~127 short 2字节 16bit 范围-2^15 -1~2^15 -1 int 4字节 32bit 范围-2^31-1 ...
- HBase0.94.2-cdh4.2.0需求评估测试报告1.0之五
根据以上图分析得出以下结论: 1. 在上面的hbase配置下,顺序和随机写记录(每条记录1K),每写入86-87M(大小可配)左右数据生成一个磁盘文件(store file). 2. 在上面的hbas ...
- P3369 【模板】普通平衡树 Treap
P3369 [模板]普通平衡树(Treap/SBT) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询 ...
- Pycharm Django开发(一)设置开发环境
一 由于我是一个对开发环境有强迫症的人,在装完PYTHON 2.6 3.3 3.4中,在创建Django工程的时候,会出现N个版本的python,那么在这里可以设置你喜欢和要使用的版本.
- 树状数组 - BZOJ 1103 [POI2007]大都市
bzoj 1103 [POI2007]大都市 描述 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员 Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. ...
- Java生产者消费者模式
为什么要使用生产者和消费者模式 在线程世界里,生产者就是生产数据的线程,消费者就是消费数据的线程.在多线程开发当中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能 ...
- 设计模式(十九)观察者模式 Observer
模拟场景: 甲方提供了一个气象站的接口,气象站上面装有:温度感应装置.湿度感应装置.气压感应装置. 现在我们是乙方,需要设计一个 WeatherData 对象,从气象站获取数据,并且利用这些数据,更新 ...