给定一个包含红色、白色和蓝色,且含有 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. eclipse如何在不联网的情况下引入dtd约束文件

    1. 获取dtd文件,解压 F:\Java配置文件\Mybatis\mybatis-3.3.0\mybatis-3.3.0.jar\org\apache\ibatis\builder\xml\ 路径下 ...

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

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

  3. Codeforces Gym 101190 NEERC 16 .L List of Primes(递归)

    ls特别喜欢素数,他总是喜欢把素数集合的所有子集写下来,并按照一定的顺序和格式.对于每一个子集,集合内 的元素在写下来时是按照升序排序的,对于若干个集合,则以集合元素之和作为第一关键字,集合的字典序作 ...

  4. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  5. 休假回来 更博-MySQL以月为单位的客户综合情况表_20161008

    十一休假老家事比较多 未来得及更新 今起依旧更博- 生成一个以用户ID为单位,各月下单天次,各月买了几个产品,各月订单额 ,天次,,天次,,天次,NULL)) AS 9月天次 FROM ( SELEC ...

  6. 「LOJ#10068」「一本通 3.1 练习 3」秘密的牛奶运输(次小生成树

    题目描述 Farmer John 要把他的牛奶运输到各个销售点.运输过程中,可以先把牛奶运输到一些销售点,再由这些销售点分别运输到其他销售点. 运输的总距离越小,运输的成本也就越低.低成本的运输是 F ...

  7. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  8. 【LeetCode】031. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  9. 选择排序(java)

    选择排序的思想是:内外两层循环,第一层循环从第一个数开始到倒数第一个数, 第二层循环从上一层的数开始, 与上一层循环的数比较,如果小于则交换位置. 代码如下: public class SelectS ...

  10. FlexPaper+SwfTools实现的在线文档功能

    最近一个项目需要实现一个在线浏览文档的功能.准备使用FlexPaper配合Pdf2Swf实现. 主要需求在于: ➔ 文档页数很多,少则几百页,多则上千页.    ➔ 相应的文档大小也在50MB以上. ...