给定一个包含红色、白色和蓝色,且含有 n 个元素的数组,对它们进行排序,使得相同颜色的元素相邻,颜色顺序为红色、白色、蓝色。
此题中,我们使用整数 0, 1 和 2 分别表示红色,白色和蓝色。
注意:
不能使用代码库中的排序函数来解决这道题。
进阶:
一个相当直观的解决方案是使用计数排序的 two-pass 算法。
首先,迭代计算出0,1 和 2 元素的个数,然后重写当前数组。
你能想出一个仅使用恒定空间的 one-pass 算法吗?
详见:https://leetcode.com/problems/sort-colors/description/

Java实现:

方法一:计数排序思想

class Solution {
public void sortColors(int[] nums) {
int i=0;
int j=0;
int k=0;
for(int p=0;p<nums.length;++p){
if(nums[p]==0){
++i;
}else if(nums[p]==1){
++j;
}else{
++k;
}
}
for(int p=0;p<nums.length;++p){
if(p<i){
nums[p]=0;
}else if(p>=i&&p<(i+j)){
nums[p]=1;
}else{
nums[p]=2;
}
}
}
}

方法二:

设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.
然后使用i从头到尾扫一遍,直到与right相遇。
i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。
需要注意的是:由于left记录第一个1的位置,因此nums[left]与nums[i]交换后,nums[left]为0,nums[i]为1,因此i++;
而right记录第一个非2的位置,可能为0或1,因此nums[right]与nums[i]交换后,nums[right]为2,nums[i]为0或1,i不能前进,要后续判断。
由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2

class Solution {
public void sortColors(int[] nums) {
int left=0;
int right=nums.length-1;
int i=0;
while(i<=right){
if(nums[i]==0){
swap(nums,i,left);
++left;
++i;
}else if(nums[i]==1){
++i;
}else{
swap(nums,i,right);
--right;
}
}
}
private void swap(int[] nums,int i,int j){
int tmp=nums[i];
nums[i]=nums[j];
nums[j]=tmp;
}
}

参考:https://www.cnblogs.com/ganganloveu/p/3703746.html

075 Sort Colors 分类颜色的更多相关文章

  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. Sort Colors,颜色排序

    问题描述:Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

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

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  4. Java for LeetCode 075 Sort Colors

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

  5. 【LeetCode】075. Sort Colors

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

  6. 37.Sort Colors(颜色排序)

    Level:   Medium 题目描述: Given an array with n objects colored red, white or blue, sort them in-place s ...

  7. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  8. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  9. Lintcode: Sort Colors II

    Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...

随机推荐

  1. C语言中的指针(二)

    指针指向谁,就把谁的地址赋给指针,指针变量和指针指向的内存变量是不一样的.不停的给指针赋值,相当于是不断的改变指针的指向. 在开发中要避免野指针的存在,在指针使用完毕之后,记得要给指针赋值成为NULL ...

  2. 第六章-jQuery

    jQuery的理念是: 写更少的代码, 完成更多的工作 jQuery有两个版本1.x和2.x, 版本2.x不再支持IE678 jQuery最明显的标志就是$, jQuery把所有的功能都封装在了jQu ...

  3. Ubuntu 16.04上编译SkyEye的测试程序

    一.首先确保Ubuntu系统上已经安装了Skyeye.skyeye-testsuite和arm-linux-gcc交叉编译工具链,如果没有安装请参考: 1.Skyeye的安装:http://www.c ...

  4. 百度之星2017初赛A

    雪崩,没晋级,补题 1001 分析:求n-1的约数个数 #include "iostream" #include "cstdio" #include " ...

  5. UltraEdit注册机原理简单说明

    UltraEdit注册机原理 By:大志若愚 UltraEdit 是 Windows 下一款流行的老牌文本/HEX 编辑器(非开源).UltraEdit 正被移植到 Linux 平台.该移植名为 UE ...

  6. Mysql常用命令行大全(一)

    登录到mysql中,然后在mysql的提示符下运行下列命令,每个命令以分号结束. 1. 显示数据库列表. show databases; 缺省有两个数据库:mysql和test. mysql库存放着m ...

  7. RedisDesktopManager 可视化工具提示:无法加载键:Scan..

    原因是redis的版本过低,window下的redis-cli.exe客户端输入 info 命令可看到该redis的版本,这个scan查看要redis2.80版本以上!!!!

  8. linux下安装Drcom

    环境:台式机物理机,centos7 因为要下载依赖包,物理机一开始没有网络,所以我先使用的是实验室的公用ip,然后完成以下操作(网上有大神说,可以现在其他机器上下载依赖包,copy过来也可以,但我没有 ...

  9. hdu1853 Cyclic Tour (二分图匹配KM)

    Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Total ...

  10. uva 12452 Plants vs. Zombies HD SP (树DP)

    Problem I: Plants vs. Zombies HD Super Pro Plants versus Zombies HD Super Pro is a game played not a ...