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 ...
随机推荐
- css中选择器
css中常用的选择器有: 1.元素选择器:h1{} 如<h1></h1> 2.类选择器:.test{}或者h1.test{} 如<h1 class="test ...
- URL转义字符
[URL转义字符] 参考:http://www.cnblogs.com/jiunadianshi/articles/2353968.html
- keras—多层感知器MLP—MNIST手写数字识别
一.手写数字识别 现在就来说说如何使用神经网络实现手写数字识别. 在这里我使用mind manager工具绘制了要实现手写数字识别需要的模块以及模块的功能: 其中隐含层节点数量(即神经细胞数量)计算 ...
- 10.Regular Expression Matching (String; Back-Track,DP)
Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...
- 清明梦超能力者黄YY(idx数组)
清明梦超能力者黄YY https://www.nowcoder.com/acm/contest/206/I 题目描述 黄YY是一个清明梦超能力者,同时也是一个记忆大师.他能够轻松控制自己在梦中的一切, ...
- 【校招面试 之 C/C++】第10题 C++不在构造函数和析构函数中调用虚函数
1.不要在构造函数中调用虚函数的原因 在概念上,构造函数的工作是为对象进行初始化.在构造函数完成之前,被构造的对象被认为“未完全生成”.当创建某个派生类的对象时,如果在它的基类的构造函数中调用虚函数, ...
- JSP标准标签库JSTL
1.什么是JSTL? JSP标准标签库(JSP Standard Tag Library) 2.JSTL标准标签库中的常用标签 JSTL是JSP页面的标签库,实质上是一段Java代码.我们常用的是它的 ...
- jquery正则判断字符串有几个逗号
var angelweb="我,你,ta,";var re=/[,,]/g;if(re.test(angelweb)){ var n=angelweb.match(re).leng ...
- yii2中的rules 自定义验证规则详解
yii2的一个强大之处之一就是他的Form组件,既方便又安全.有些小伙伴感觉用yii一段时间了,好嘛,除了比tp"难懂"好像啥都没有. 领导安排搞一个注册的功能,这家伙刷刷刷的又是 ...
- install package
http://www.michael-noll.com/blog/2014/03/17/wirbelsturm-one-click-deploy-storm-kafka-clusters-with-v ...