Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:
You are not suppose to use the library's sort function for this problem.

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?


【题目分析】

一个数组中用0,1,2分别代表三种颜色red,white,blue。对这三种颜色排序,使得red在最前面,blue在最后面。最好的办法是一次遍历,使用固定的空间。


【思路】

1. 排序法

这是效率最低的方法,我们要像排序其他数组一样对有三种值(0,1,2)的数组增序排序,时间复杂度在O(n2);

2. 计数法

先遍历一遍数组,统计每种颜色的个数m,n,l,然后进行第二次遍历,写入m个0,n个1,l个2;时间复杂度为O(n);

3. 标记法

我们设置两个标记,分别记录0的结束位置和2的开始位置,遍历当前数组,如果为1则继续向前,如果为0则把它放到0的结束位置之后,如果为2就把它放到2的开始位置之前。这样经过一遍遍历就可以把不同的颜色分开。举个例子如下:


【java代码】

 public class Solution {
public void sortColors(int[] nums) {
if(nums == null || nums.length == 0) return; int lastred = -1;
int firstblue = nums.length;
int i = 0; while(i < firstblue){
if(nums[i] == 0){
if(i == lastred + 1)
lastred = i++;
else if(i > lastred + 1){
nums[++lastred] = 0;
nums[i++] = 1;
}
}
else if(nums[i] == 2){
if(nums[firstblue-1] != 2){
nums[i] = nums[firstblue-1];
nums[--firstblue] = 2;
}
else firstblue--;
}
else i++;
}
}
}

LeetCode OJ 75. Sort Colors的更多相关文章

  1. 【LeetCode】75. Sort Colors (3 solutions)

    Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...

  2. 【一天一道LeetCode】#75. Sort Colors

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  4. LeetCode OJ:Sort Colors(排序颜色)

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

  5. LeetCode OJ平台Sort Colors讨论主题算法

    原题如下面,这意味着无序排列(由0,1,2组成).一号通.组织成若干阵列0-几个1-几个2这样的序列. Given an array with n objects colored red, white ...

  6. 【leetcode】75. Sort Colors

    题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...

  7. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

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

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

  9. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

随机推荐

  1. hdu 1536 S-Nim (简单sg函数)

    题意:首先输入K 表示一个集合的大小  之后输入集合 表示对于这对石子只能去这个集合中的元素的个数 之后输入 一个m 表示接下来对于这个集合要进行m次询问 之后m行 每行输入一个n 表示有n个堆  每 ...

  2. fpga串口通信

    ---恢复内容开始--- 1.波特率的计算公式:9600bps 是指每秒可以传输9600位 则一位需要的时间为1/9600 约等于0.000104 开发板晶振大小为50M则传输一位需要的时间为 0.0 ...

  3. PHPStorm 安装 SASS、SCSS + Compass

    许久没更新博客啦,这两天研究了下 SASS 和 LESS ,最终选了 SASS,因为相对比较成熟些吧,试了很多坑之后,终于成功了,下面上步骤: 1. 安装 PHPStorm 的 SASS 插件 好像是 ...

  4. 博客搬到CSDN了,以后就老实的呆在这儿吧~~

    几年前读书的时候就自己在做独立的个人博客网站,重做 + 改版好多次,域名也换了好几个- 163fly.com.godbz.com.zhouz.me ... 都是我曾经用过的域名,都放弃了- 发现到头来 ...

  5. Angular2 + NativeScript 跨平台开发笔记(一)

    NativeScript 是一款跟 ReactNative 对着怼的移动开发技术,其官方钦定了 Angular2 作为推荐的技术框架,那么如何让在浏览器中运行的 Angular2 Web app 项目 ...

  6. 第一百二十二节,JavaScript表单处理

    JavaScript表单处理 学习要点: 1.表单介绍 2.文本框脚本 3.选择框脚本 为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面. 一. ...

  7. webstorm之js,css文件压缩

    不说废话了,代码压缩是每个网站上线前的必备过程,但由于有时候小的项目或者加急的项目每次都构建一次类似gulp或者grunt等前端部署,实在是大题小做,所以才有了今天这个帖子: 我们会用到yui com ...

  8. nsstring遍历汉子

    NSString *mytimestr=@"好人一生平安"; size_t length = [mytimestr length]; ; i < length; i++) { ...

  9. rebase

    /BASE (Base Address) https://msdn.microsoft.com/en-us/library/f7f5138s.aspx Need for Rebasing a DLL( ...

  10. react学习笔记-01

    1. HTML模板 Jsx是react的语法糖,最终会被编译成js语法.因此需要第三方库browser将jsx转换成js. 由于react 0.14版本之后,将react和react-dom拆分,所以 ...