给定一个具有红色,白色或蓝色的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. Android中decode JPG时建议使用inPreferQualityOverSpeed

    在BitmapFactory.decodeBitmap方法中,参数BitmapFactory.Options里有一项是inPreferQualityOverSpeed:设为true的话,画质更好,加载 ...

  2. ubuntu安装openssh-server 报依赖错误的解决过程

    ubuntu自带的有openssh-client,所以可以通过 ? 1 ssh username@host 来远程连接linux 可是要想通过ssh被连接,ubuntu系统需要有openssh-ser ...

  3. jenkins 自动构建gitlab项目

    安装的plugin: - kubernetes:1.7.1    - workflow-aggregator:2.5    - workflow-job:2.21    - credentials-b ...

  4. 获取iframe内的元素

    $("#iframeID").contents().find("#index_p") 2获取父窗体的值 $('#father', parent.document ...

  5. Centos7 配置ssh 免秘钥登陆

    1.yum install -y openssh 2.servier1: ssh-keygen -t rsa #有提示的直接enter 3.server 2: ssh-keygen -t rsa # ...

  6. 用R包来下载sra数据

    1)介绍 我们用SRAdb library来对SRA数据进行处理. SRAdb 可以更方便更快的接入  metadata associated with submission, 包括study, sa ...

  7. mydqldump

    [mydqldump] One way to create a snapshot of the data in an existing master database is to use the my ...

  8. 部分PR回写的数量带有小数,分别是2023工厂的纸箱104007000389,2021工厂的纸盒404002005930;

    描述:部分PR回写的数量带有小数,分别是2023工厂的纸箱104007000389,2021工厂的纸盒404002005930: 原因:所有物料规划PR时对舍入值的先后考虑逻辑影响到回写出来的temp ...

  9. python没有switch,可以用字典来替代

    python没有switch,是因为可以用字典来替代,具体方法如下: def add(x,y): print(x+y)def subtraction(x,y): print(x-y)def multi ...

  10. 第七章 二叉搜索树(c)平衡与等价