75. Sort Colors(颜色排序) from LeetCode
给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色。
这里,我们将使用整数0,1和2分别表示红色,白色和蓝色。
注意: 您不应该使用库的排序功能来解决此问题。
例:
输入: [2,0,2,1,1,0]
输出: [0,0,1,1,2,2]
跟进:
- 一个相当直接的解决方案是使用计数排序的两遍算法。
首先,迭代0,1,和2的数组计数,然后覆盖总数为0的数组,然后是1,然后是2。 - 你能想出一个只使用恒定空间的一次通过算法吗?
思路1:遍历统计0,1的个数(剩下的就是2啦),然后将0,1,2依次填入
void sortColors(int* nums, int numsSize) {
int i=,num0=,num1=,temp;
while(i<numsSize){
if(nums[i]==)
num0++;
if(nums[i]==)
num1++;
i++;
}
i=,num1=num1+num0;
while(i<numsSize){
if(i<num0)
nums[i]=;
else if(i<num1)
nums[i]=;
else nums[i]=;
i++;
}
}
void sortColors(int* nums, int numsSize) {
int count[]={};//存放0,1,2三个元素的频率
for(int i=;i<numsSize;i++){
assert(nums[i]>= && nums[i]<=);//断言,处理nums有不是0,1,2的值
count[nums[i]] ++;
int index = ;
for(int j=;j<;j++)
for(int i=;i<count[j];i++)
nums[index++] = j;
}
}
思路2:

那么接下来插入(读入)一个数,无非就三种情况,即2、1和0
情况一:插入的值为2,则不需任何操作就可以保持前面的数据结构,故可以直接处理下一个数据

情况二:插入的值为1,这是需要交换1和2来保持数据结构不变,操作如下:

情况三:插入的值为0,这是比较麻烦,因为需要分别进行0和2交换,然后0和1交换,操作如下:

void sortColors(int* nums, int numsSize) {
int i=,k0=-,k1=-,temp;
while(i<numsSize){
if(nums[i]==)i++;//2不变
else if(nums[i]==)//1和2交换
{
temp=nums[i];
nums[i++]=nums[++k1];
nums[k1]=temp;
}
else if(nums[i]==){//0和2换,再和1换
temp=nums[i];
nums[i++]=nums[++k1];
nums[k1]=nums[++k0];
nums[k0]=temp;
}
思路三:利用三路排序

void sortColors(int* nums, int numsSize) {
int zero=-,two=numsSize,i=,temp;//[0,zero]=0,[zero+1,i]=1,[two,n-1]=2
while(i<two){
if(nums[i]==)
i++;
else if(nums[i]==)
{
zero++;
temp = nums[zero];
nums[i] = temp;
// if(i!=zero) //不用temp交换的坑
// nums[i]=1;
nums[zero] = ;
i++;
}
else if(nums[i]==){
two --;
temp = nums[i];
nums[i] = nums[two];
nums[two] = temp;
}
}
}
75. Sort Colors(颜色排序) from LeetCode的更多相关文章
- [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 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 颜色排序
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- LeetCode OJ 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 判断TrueType字体
function IsTrueTypeFont(FontName : string) : boolean;const PITCH_MASK: byte = $0F; var TxMetric: ...
- js原生实现 无缝滚动图片
<!-- 无缝滚动图片开始 --> <div id=demopic style="OVERFLOW: hidden; WIDTH: 100%; COLOR: #fff; H ...
- powerdns
powerdns http://bbs.51cto.com/thread-880297-1.html https://blog.csdn.net/kepa520/article/details/791 ...
- twitter 等网站console.log不能使用,可用alert或者
function setConsole() { var iframe = document.createElement('iframe'); iframe.style.display = 'none' ...
- 基于mysql全文索引的深入理解
最近要使用mysql的全文索引,一直没能成功,一个是只有MyISAM引擎支持,创建表时需要指定,而是需要对my.ini进行配置. 前言:本文简单讲述全文索引的应用实例,MYSQL演示版本5.5.24. ...
- LuoguP1032 字符变换(BFS)
题目链接为:https://www.luogu.org/problemnew/show/P1032 思路:看到数据比较小,而且最多有6个规则,就可以用搜索去做了,我用的BFS,大体思路如下: 定义结构 ...
- DOS批处理前言
-----------made by siwuxie095 1.批处理(Batch):望文知义,对某对象进行批量处理,实际上是一种脚本 2.DOS(Disk Operating System-磁盘操作 ...
- TZOJ 4007 The Siruseri Sports Stadium(区间贪心)
描述 The bustling town of Siruseri has just one sports stadium. There are a number of schools, college ...
- jquery 动态添加的代码不能触发绑定事件
今天发现jQuery对动态添加的元素不触发事件,比如blur.click事件等 参考文章证明了我的结论,并给出了原因及解决方案 原因:程序找不到动态添加的节点. 解决方案:在绑定父元素后的子元素 $( ...
- 硬盘的 read0 read 1
Read 0:组建的时候必须2块容量相同的硬盘,每个程序的数据以一定的大小分别写在两个硬盘里,读的时候从两个硬盘里一起读,这种阵列方式理论上硬盘的读写速度是一块硬盘的2倍,实际应用中大约速度比一块硬盘 ...