给定一个包含红色、白色和蓝色,且含有 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. css设置文件编码

    在外部css文件的顶部,写入下面代码: @charset "UTF-8";

  2. 1076 Forwards on Weibo (30)(30 分)

    Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...

  3. 关于ng-class中添加多个样式类的解决方案

    想要达到ng-class的效果,有两种写法 1.class=“{{class}} class1 class2” 2.ng-class="{true: 'active', false: 'in ...

  4. 【Lintcode】098.Sort List

    题目: Sort a linked list in O(n log n) time using constant space complexity. Example Given 1->3-> ...

  5. T(n) = 25T(n/5)+n^2的时间复杂度

    对于T(n) = a*T(n/b)+c*n^k;T(1) = c 这样的递归关系,有这样的结论: if (a > b^k)   T(n) = O(n^(logb(a)));logb(a)b为底a ...

  6. 洛谷P1092虫食算——深搜

    题目:https://www.luogu.org/problemnew/show/P1092 剪枝1:从右往左.从上往下按字母出现顺序搜索: 剪枝2:同一列前两个数字确定,可直接算出第三个数字并判断: ...

  7. Azure一个Cloud Service支持多个公网地址

    Azure刚刚发布在同一个Cloud Service下支持多个公网IP地址的功能. 这个功能主要是用于: 当相同的端口需要公用相同的LoadBalance时. 比如: 一种使用场景是多组Web服务器被 ...

  8. hdu水仙花

    水仙花数 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission ...

  9. matlab下的caffe接口配置(Windows)

    本文基于大部分网上方法 http://blog.csdn.net/d5224/article/details/51916178,外加一点自己的个人实际配置经历,环境变量在配置后尽管显示正确并且重启多次 ...

  10. Auto Layout Guide----(三)-----Anatomy of a Constraint

    Anatomy of a Constraint 剖析约束 The layout of your view hierarchy is defined as a series of linear equa ...