给定一个包含红色、白色和蓝色,且含有 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. 《java编程思想》:异常丢失

    finally子句的不恰当使用,会造成异常的丢失,此处列举两种典型的错误使用示例.编程中要避免这种情况 示例一: try{ throw new ExceptionA(); }finally{ thro ...

  2. [acm]HDOJ 1200 To and Fro

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1200 简单字符串处理,找规律 /* 11509672 2014-08-21 11:32:55 Acc ...

  3. super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

    super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...

  4. Qt之log数据展示模块简要实现

    Log模块主要用于实时测井数据的显示和测后曲线数据的预览和打印,为更好的展示对Qt中相关知识点的应用,特以Log模块为例对其进行简要实现. 内容导图: 一.功能需求 1.界面效果图 Log模块实现曲线 ...

  5. bzoj 1049: 数字序列 dp

    题目大意: 给定一个长度为n的整数序列.在改变的数最小的和改变的幅度最小的前提下把它变成一个单调严格上升的序列.求改变的最小的数和这个幅度. 题解: (貌似以前考试考过这道题) 其实这道题就是两道题拼 ...

  6. mac hosts

    1 在命令行中输入:sudo vim /etc/hosts. 2 输入开机密码,就可打开文件.按下键盘i,对文件进入可编辑状态. 3 修改完,先按esc退出编辑模式,之后,按shift+:,再按wq来 ...

  7. node.js setup wizard ended prematurely Win7安装nodejs失败解决方法

    笔记本win7在nodejs官方网站下载.msi文件安装,安装到一半的时候,进度条提示:roll back,because of a error.node.JS setup wizard ended ...

  8. struts2+jquery+easyui+datagrid+j…

    一.概述 struts2提供了针对json的插件支持.常规来讲我们将如何将对象数组转成json对象在客户端直接调用呢?尤其和jquery的easyui插件配合使用,这个可能会有很多的问题需要我们解决. ...

  9. 2、CDH组件安装

    一.zookeeper 1.安装 继续->完成: 二.HDFS 1.安装 继续->完成: 三.yarn.hive 1.安装yarn 继续->完成: 2.安装hive 继续->完 ...

  10. 1、在 Windows 上安装 OpenCV-Python & ubuntu16.04安装 opencv

    Goals In this tutorial We will learn to setup OpenCV-Python in your Windows system. Below steps are ...