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 ...
随机推荐
- unity 查找脚本被场景中哪些对象引用
在需要查找的脚本上右键: 在场景中已经显示出所有引用该脚本的对象
- Mysql count+if 函数结合使用
Mysql count+if 函数结合使用 果林椰子 关注 2017.05.18 13:48* 字数 508 阅读 148评论 0喜欢 1 涉及函数 count函数 mysql中count函数用于统计 ...
- 迷你MVVM框架 avalonjs 学习教程17、avalon的一些配置项
本章节,主要是介绍avalon.config方法,通过它来制定一些更贴心的功能. 一般情况下,我们在使用ms-controller绑定时,需要添加一个ms-controller类名,目的是为了防止网速 ...
- 将Delphi的对象方法设为回调函数
心血来潮,为了实现更好的通用性和封装性,需要把类方法作为回调函数,搜得一篇好文,节选转发.命名似乎应该是MethodToCallback才合适,可惜调试时总是报错,debugging. 原文地址:ht ...
- pandas 一行文本拆多行,一列拆多列
https://zhuanlan.zhihu.com/p/28337202 一列拆多列: http://blog.csdn.net/qq_22238533/article/details/761875 ...
- scala spark rdd转数据框
1. http://blog.csdn.net/lw_ghy/article/details/51480358
- python网络编程——socket基础篇
python的网络编程比c语言简单许多, 封装许多底层的实现细节, 方便程序员使用的同时, 也使程序员比较难了解一些底层的东西. 1 TCP/IP 要想理解socket,首先得熟悉一下TCP/IP协议 ...
- 使用HTTP头去绕过WAF(bypasswaf)
在用node http get 请求的时候,发现的 解决方案: Add headers to all Burp requests to bypass some WAF products. This e ...
- Linux查看内存使用情况
输入:top PID:进程的ID USER:进程所有 PR:进程的优先级别,越小越优先被执 NInice: VIRT:进程占用的虚拟内 RES:进程占用的物理内 SHR:进程使用的共享内 S:进程的状 ...
- Gradle with Android
[Gradle with Android] The Android Studio build system is based on Gradle, and the Android plugin for ...