给定一个具有红色,白色或蓝色的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的更多相关文章

  1. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  2. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. LeetCode 75. Sort Colors(排序颜色)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  4. [LeetCode] Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  5. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  6. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  8. 刷题75. Sort Colors

    一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...

  9. 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 ...

随机推荐

  1. ssh反向连接内网主机

    holer听别人说也挺好用不过本人没试过:https://github.com/Wisdom-Projects/holer 利用autossh建立稳定隧道,前提双方互加公钥信任. # yum inst ...

  2. QQ消息无限发送!源代码

    昨天我一个朋友发给我一个特别有趣的程序 可以无限发送QQ消息,非常有趣! 发送给朋友之后只要打开,便可自动发送消息. 点打开后 便可一直发送消息 用Edit plus 打开后  其源代码如下 是用VB ...

  3. destoon 分页

    php: global $pagesize,$page; $pagesize = 10;//分页改为10条一页 $offset or $offset = ($page-1)*$pagesize; $t ...

  4. 如何获取某个网站的favicon.ico

    http://moco.imooc.com/player/report.html 今天看到这个网站上,左侧的小图片挺好看的,想弄下来,检查源码,也没有看到 <head> <meta  ...

  5. Linux Tomcat重新启动

    在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...

  6. Access to the path 'C:\inetpub\wwwroot\mysite\images\savehere' is denied.

    访问路径被拒绝 我解决了这个设置: IIS>应用程序池> [您的站点]>高级设置...>标识>内置帐户> LocalSystem

  7. 【英宝通Unity4.0公开课学习 】(三)脚本使用

    清明出去放松了一天. 看了下大姑爷,然后去大姑家吃了个午饭,下午三点左右出去找煤球耍,在他们学校和良乡镇逛了一下.当时感觉离北京好远好远啊... 其实不得不说现在的交通确实很方便,到哪都要不了几天,如 ...

  8. phpStudy6——php导出可以设置样式的excel表格

    前言: 一般的后台管理页面肯定少不了excel表格导出的功劳,尤其是那些电商平台的订单导入导出,用户列表的导入导出等,那么本文就介绍php是如何导出excel表格的. php导出excel方法有很多, ...

  9. C++ 内存解析

    一.内存基本构成可编程内存在基本上分为这样的几大部分:静态存储区.堆区和栈区.他们的功能不同,对他们使用方式也就不同. 静态存储区:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在 ...

  10. springMVC将处理的后的数据通过post方法传给页面时,可能会出现乱码问题,下面提出解决post乱码问题的方法

    在web.xml中加入: <!-- 解决post乱码问题 --> <filter> <filter-name>CharacterEncodingFilter< ...