前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

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?

2. 题意

 

从题目的意思不难看出,需要将一个随机的颜色序列排序,按照red, white, blue 三种颜色排序。

 

3. 思路

从题目的意思不难看出,需要将一个随机的颜色序列排序,按照red, white, blue 三种颜色排序。
由于输入序列中只有三种固定的颜色,那么最容易想到的办法,就是计数排序,Counting Sort。
 
 1     void sortColors(int A[], int n) {
2 int s0,s1,s2,i;
3 s0=s1=s2=0;
4 for(i=0;i<n;i++)
5 {
6 if(A[i]==0) s0++;
7 else if(A[i]==1) s1++;
8 else s2++;
9 }
10 for(i=0;i<s0;i++) A[i]=0;
11 for(i=s0;i<s0+s1;i++) A[i]=1;
12 for(;i<n;i++) A[i]=2;
13 }

然而,正如Follow up中所表述的一样,计数排序需要两次遍历数组,那么我们能否找到一种只需要遍历一次数组,就能将其排序的方法呢?

 

4: 解法

由于只有三种颜色,那么红色必然在数组的左边,而蓝色必然在数组的右边。

那么我们只需要两个变量记录红色所在区域的边界[0, i], 以及蓝色所在区域的边界[j,n-1]。那么白色所在的区域必然为(i,j).

怎样得到红蓝两色的边界呢?

初始化: 红色边界i=0; 蓝色边界j=n-1;

为了加速运算,可以预处理,分别从左至右,从右至左,找到红蓝边界,缩小搜索范围。见代码line[3,4]

假设当前位置为k

(1) A[k] 为红色, 那么将该元素同红色右边界的后一个数互换。 A[k] ~ A[i++]

(2) A[k] 为蓝色, 那么将该元素同蓝色左边界的前一个数互换。 A[k] ~ A[j--]

(3) A[k] 为白色, 那么当前无需交换, k=k+1;

终止条件 k>j 此时不可能出现白色,可以退出了。

该过程时间复杂度 O(n).

 1     void sortColors(int A[], int n) {
2 int i=0,j=n-1,k;
3 while(A[i]==0) i++;
4 while(A[j]==2) j--;
5
6 k=i;
7 while(k<=j)
8 {
9 if(A[k]==0)
10 {
11 if(A[i]==0)
12 k++;
13 else
14 swap(A[i],A[k]);
15 i++;
16 }
17 else if(A[k]==2)
18 {
19 swap(A[j],A[k]);
20 --j;
21 }
22 else
23 k++;
24 }
25 }

 

作者:Double_Win

出处: http://www.cnblogs.com/double-win/p/3759765.html

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode题解]: Sort Colors的更多相关文章

  1. [Leetcode Week2]Sort Colors

    Sort Colors题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/sort-colors/description/ Description Give ...

  2. 【LeetCode】Sort Colors 数组排序

    题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...

  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】Sort Colors

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

  5. LeetCode 75. Sort Colors(排序颜色)

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

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

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

  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】Sort Colors(middle)☆

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

  9. 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 ...

随机推荐

  1. 获取用户的相关请求信息, 以及包括请求头 request.environ

    #在index文件中 1. print(type(request)) #看出所属库 2. from django.core.handlers.wsgi import WSGIRequest #查看WS ...

  2. ajax传递给后台数组参数方式

    出自:http://blog.csdn.net/lingxyd_0/article/details/10428785 在项目上用到了批量删除与批量更改状态,前台使用了EasyUI的DataGrid,用 ...

  3. 7. H.264的句法和语义

    1.句法 在编码器输出的码流中,数据的基本单位是句法元素,每个句法元素由若干比特组成,它表示某个特定的物理意义,例如:宏块类型.量化参数等. 句法表征句法元素的组织结构,语义阐述句法元素的具体含义. ...

  4. Abstract(抽象)

    谈到抽象,就先谈谈面向对象语言的三大特性,也是人们口中常说的封装.继承.多态. 封装:什么是封装,按到我的理解,封装就是把某些类的相关属性和方法封装,对内实现数据影城,对外提供稳定接口. 继承:从字面 ...

  5. 使用C#通过Oracle.DataAccess连接Oracle,部署时需要注意版本问题

    平时我们开发使用的是32位的PC机,所以安装的也是Oracle32位的客户端.但是一般服务器都是64位的,安装的也是64位的Oracle客户端,如果要部署使用Oracle.DataAccess连接Or ...

  6. Spark之 SparkSql、DataFrame、DataSet介绍

    SparkSql SparkSql是专门为spark设计的一个大数据仓库工具,就好比hive是专门为hadoop设计的一个大数据仓库工具一样. 特性: .易整合 可以将sql查询与spark应用程序进 ...

  7. Java 8特性

    1. Java8的新特性 1.1. Lambda表达式和函数式接口 最简单的Lambda表达式可以用逗号分隔的参数列表.->符号和功能语句块来表示.示例如下: Arrays.asList( &q ...

  8. Windows下redis的安装与使用

    Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...

  9. Boost线程详解

    一.创建一个线程 创建线程 boost::thread myThread(threadFun); 需要注意的是:参数可以是函数对象或者函数指针.并且这个函数无参数,并返回void类型. 当一个thre ...

  10. 洛谷 P2569[SCOI2010]股票交易(动规+单调队列)

    //只能写出裸的动规,为什么会有人能想到用单调队列优化Orz 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测 ...