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.

click to show follow up.

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?


题目标签:Array

  这道题目给了我们一个颜色的array,让我们sort一下,按照0,1,2的顺序。根据题目要求,我们只能遍历array一次,可以用到two pointers来实现。设一个指针red 在开头,blue 在最后。想法就是,遇到红色0,就交换,把0放到最左边去;遇到蓝色2就交换,把2都放到最右边去,这样1就会被保留在最中间。需要注意的是,当把蓝色2交换完毕之后,需要i--, 停留 i 在原地一次,因为还需要继续检查 被2交换回来的数字。那当遇到红色0,交换完毕不需要停留i 的原因是, 交换回来的只可能是1,对于1,我们不需要做任何处理,直接过就可以。
 
 
 

Java Solution:

Runtime beats 55.91%

完成日期:07/24/2017

关键词:Array

关键点:用two pointers,一头一尾放置红色和蓝色,保留白色在中间

 public class Solution
{
public void sortColors(int[] nums)
{
int red = 0;
int blue = nums.length-1; for(int i=0; i<=blue; i++)
{
if(nums[i] == 0) // if find 0, swap with red pointer
{
int temp = nums[i];
nums[i] = nums[red];
nums[red] = temp; red++;
}
else if(nums[i] == 2) // if find 2, swap with blue pointer
{
int temp = nums[i];
nums[i] = nums[blue];
nums[blue] = temp; i--;
blue--;
} }
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4341243.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 75. 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. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  3. [LeetCode] 75. Sort Colors 颜色排序

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

  4. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  5. [leetcode]75. Sort Colors三色排序

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

  6. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  7. Leetcode 75. Sort Colors

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

  8. leetcode 75. Sort Colors (荷兰三色旗问题)

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

  9. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

随机推荐

  1. node.js的安装及配置

    一.安装 直接在浏览器搜索node.js,在官网上下载(一般旧版的更加稳定,比如下载4.4.7版本) 点击DOWNLOADS 往下翻,点击Previous Release Windows下载msi(6 ...

  2. Spring第五篇【cglib、手动实现AOP编程】

    前言 到目前为止,已经简单学习了Spring的Core模块.也会怎么与Struts2框架进行整合了-.于是我们就开启了Spring的AOP模块了-在讲解AOP模块之前,首先我们来讲解一下cglib代理 ...

  3. SQL基础巩固

    1.一定要记住,SQL 对大小写不敏感! 2.分号是在数据库系统中分隔每条 SQL 语句的标准方法,这样就可以在对服务器的相同请求中执行一条以上的语句. 如果您使用的是 MS Access 和 SQL ...

  4. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南 | ApacheCN

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  5. NET_NET深入体验与实战 第一章 .NET你知道 1.1什么是 .NET

    1.1什么是 .NET 1.微软定义:Microsft.NET 是微软以  Web Service为核心的,支持 信息,人,系统的一组软件产品,技术或者服务. 2.战略和梦想:(1) Microsft ...

  6. 利用ASP.NET操作IIS (可以制作安装程序)

    很多web安装程序都会在IIS里添加应用程序或者应用程序池,早期用ASP.NET操作IIS非常困难,不过,从7.0开始,微软提供了 Microsoft.Web.Administration 类,可以很 ...

  7. StringBuffer的替换和反转和截取功能

    A:StringBuffer的替换功能 * public StringBuffer replace(int start,int end,String str): * 从start开始到end用str替 ...

  8. StringBuffer类的构造方法

    public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象(默认是16个字符) public String ...

  9. [原创]MinHook测试与分析(x64下 E9,EB,CALL指令测试,且逆推测试微软热补丁)

    依稀记得第一次接触Hook的概念是在周伟民先生的书中-><<多任务下的数据结构与算法>>,当时觉得Hook很奇妙,有机会要学习到,正好近段日子找来了MiniHook,就一 ...

  10. 入坑IT都快十年了

    一起帮的开发直播已经告一段落:一是主体的功能差不多都实现了,二是用到的架构技术都展示得差不多了.以后就算继续开发,也应该都是一些“技术上”重复的工作而已.整个直播过程耗时近半年,SVN提交1062次, ...