给定一个包含红色、白色和蓝色,且含有 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. hdu 6103(Kirinriki)

    题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...

  2. bzoj 4066: 简单题 K-D树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=4066 题解 我们把每次的修改操作都当作二维平面上多了一个权值点 对于每组询问可以看做求一 ...

  3. 解决mongodb查询慢的问题

    最近项目上一直在用mongodb作为数据库,mongodb有他的优势,文档型类json格式存储数据,修改起来比传统的关系型数据库更方便,但是最近在用mongodb出现了查询缓慢的问题,我用命令行查询, ...

  4. 搭建基于Nagios的监控系统——之监控远程Linux服务器

    上一篇介绍了如何安装Nagios Core,这一篇跟大家分享一下如何将一台远程的Linux服务器加入纳入监控范围. 第一部分:在远程Linux上安装Nagios Plugins和NRPE   第一步: ...

  5. Spring 源码解析之DispatcherServlet源码解析(五)

    spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一 ...

  6. 13 vue学习 package.json

    一:package.json文件详解 管理你本地安装的npm包 .定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据).npm install命令根据这个配置文件,自动 ...

  7. Sleep 比对 (Win32API 与 STL )

    OutputDebugStringA("begin 1========"); for (int i = 0; i < 1800; i++) { Sleep(2); } Out ...

  8. 【何镇汐】-Web UI Util 框架

    http://www.cnblogs.com/xiadao521/p/4518516.html

  9. try-catch-finally中return的执行情况

    在try中没有异常的情况下try.catch.finally的执行顺序 try--- finally 如果try中有异常,执行顺序是try--- catch --- finally 如果try中没有异 ...

  10. js 常用验证

    邮箱验证: $(function () {            $("#txt_Email").blur(function () {                var ema ...