Sort Colors

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?

不看题目限制,大家都能想到的就是计数排序了。需要扫两遍,一遍数,一遍赋值。

【最简单:计数排序】

class Solution {
public:
void sortColors(int A[], int n) {
int i = ;
int j = ;
int k = ;
for(int p = ; p < n; p ++)
{
if(A[p] == )
{
i ++;
}
else if(A[p] == )
{
j ++;
}
else
k ++;
} for(int p = ; p < n; p ++)
{
if(p < i)
A[p] = ;
else if(p >= i && p < i + j)
A[p] = ;
else
A[p] = ;
}
}
};

如果只能扫一遍,很容易想到的就是左边存放0和1,右边存放2.两边往中间靠。

设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.

然后使用i从头到尾扫一遍,直到与right相遇。

i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。

需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此i++;

而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断。

由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2

0  0  0  1  1  1  2  1  0  2  1  2  2  2

^         ^             ^

left         i            right

【最直接:partition】

class Solution {
public:
void sortColors(int A[], int n) {
int left = ;
int right = n-;
int i = ;
while(i <= right)
{
if(A[i] == )
{
swap(A[left], A[i]);
left ++;
i ++;
}
else if(A[i] == )
{
i ++;
}
else
{
swap(A[i], A[right]);
right --;
}
}
}
};

这是网上看到一种漂亮的做法,膜拜。

【最直观:平移插入】

class Solution {
public:
void sortColors(int A[], int n) {
int i = -;
int j = -;
int k = -;
for(int p = ; p < n; p ++)
{
//根据第i个数字,挪动0~i-1串。
if(A[p] == )
{
A[++k] = ; //2往后挪
A[++j] = ; //1往后挪
A[++i] = ; //0往后挪
}
else if(A[p] == )
{
A[++k] = ;
A[++j] = ;
}
else
A[++k] = ;
} }
};

【LeetCode】75. Sort Colors (3 solutions)的更多相关文章

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

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

  2. 【leetcode】75. Sort Colors

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

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

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

  4. 【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】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  6. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  7. LeetCode OJ 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】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  9. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

随机推荐

  1. 利用c#+jquery+echarts生成统计报表(附源代码)

    背景: 因为最近项目要生成报表,经过几轮挑选,最终选择了百度的echarts作为报表基础类库.百度echarts简介请参考 http://echarts.baidu.com/ 虽然echarts功能强 ...

  2. 如何获取隔壁wifi密码,非暴力破解

    目前常见的Wi-Fi加密方式有WEP.WPA2和WPS(链接为各自的破解方式),不过有网友反映以往破解WPA2的方法耗时太长,而且不适用于所有WPS启动的接入点.而今天介绍的这种方法则更加省时省力. ...

  3. Visual Studio 2013键盘码农常用快捷键

    声明及广告 所有功能针对C#开发配置而写,面向人群是喜欢键盘操作而非鼠标操作的开发者.部分功能可能由插件提供,我会尽可能标注出相应的插件名称.为行文方便,所有快捷键以大写形式表示.太常用的快捷键,如C ...

  4. C语言:结构体和联合体(共用体)

    结构体:struct 1.结构体变量的首地址能够被其最宽基本类型成员的大小所整除. 2.结构体每个成员相对于结构体首地址的偏移量(offset)都是成员的整数倍. 3.结构体的总大小为结构体最宽基本类 ...

  5. Vue Cli安装以及使用

      因为公司项目要用vue框架,所以会用vue-cli来新建项目.用过vue的都知道,要全局安装vue以及脚手架vue-cli,然后执行vue init webpack projectname来新建v ...

  6. go语言基础之二维数组

    1.二维数组 示例: package main //必须有个main包 import "fmt" func main() { //有多少个[]就是多少维 //有多少个[]就用多少个 ...

  7. 【系统】supervisor支持多进程

    [program:deployworker] directory = /etc/ansible/easyAnsible/app/deploy/ command = python Deploy.py p ...

  8. Ubuntu16.04下Neo4j图数据库官网安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...

  9. dubbo+maven多模块项目单元测试

    基本上就是记录各种报错的解决办法.基本上就是将散落在项目各个模块中的配置文件复制到测试模块中. 目录结构: ——src ——java ——test ——java ——DaoTest.java ——re ...

  10. Java 调用Web service 加入认证头(soapenv:Header)

    前言 有时候调用web service 会出现 Message does not conform to configured policy [ AuthenticationTokenPolicy(S) ...