题目描述

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?

即将0、1、2的序列按顺序排好。
【扫描两遍的计数排序】

 public class Solution {
public void sortColors(int[] A) {
int i, r, w, b;
r = w = b = 0;
for (i = 0; i < A.length; i++) {
if (A[i] == 0) r++;
else if (A[i] == 1) w++;
else b++;
}
for (i = 0; i < A.length; i++) {
if (i < r) A[i] = 0;
else if (i < r + w) A[i] = 1;
else A[i] = 2;
}
}
}

【扫描一遍,单向遍历】

注意,l记录0区,r记录2区的边界。因此循环遍历到i<=r即可。

 class Solution {
public:
void swap(int A[], int i, int j){
int tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
void sortColors(int A[], int n) {
int l=,r=n-;
for(int i=;i<=r;i++){
if(A[i]==){
swap(A,i,l);
l++;
}
else if(A[i]==){
swap(A,i,r);
r--;
i--;
}
}
}
};

sort-colors——排序3种数字的更多相关文章

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

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

  2. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  3. 【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 ...

  4. 【LeetCode】Sort Colors 解题报告

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

  5. 【LeetCode】Sort Colors 数组排序

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

  6. Sort colors系列

    75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第 ...

  7. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  8. Sort Colors I & II

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

  9. [Leetcode Week2]Sort Colors

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

随机推荐

  1. win8下notepad++无法设置文件关联

  2. Grep查看日志的方法【转】

    转自:http://blog.csdn.net/stormkey/article/details/5905204 版权声明 :转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://go ...

  3. vim的最最基本配置

    全部用户生效 /etc/vimrc 当前用户生效 ~/.vimr # 1.设置语法高亮syntax on # 2.显示行号 set nu # 3.设置换行自动缩进为4个空格 # 4.设置tab缩进为空 ...

  4. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  5. (寒假集训)Watering the Fields (最小生成树)

    Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, ...

  6. Flash3D学习计划(一)——3D渲染的一般管线流程

    一:什么是渲染管线 渲染管线也称为渲染流水线,是显示芯片内部处理图形信号相互独立的并行处理单元.一个流水线是一序列可以并行和按照固定顺序进行的阶段.每个阶段都从它的前一阶段接收输入,然后把输出发给随后 ...

  7. DQL数据查询语言——连接查询

    --内连接 两种写法 等值连接select r.*,b.bummc from t_hq_ryxx r, t_hq_bm b where r.bumbm = b.bumbm select r.*,b.b ...

  8. 集合框架(04)HashMap

    集合Map的简单方法:该集合存储键值对,一对一对往里面存,而且要保证健的唯一性 1.添加 put(K key,V value) putAll(Map<? Extends k, ? extends ...

  9. 用kermit通过串口往nandflash任意地址里烧写任何文件!

    1.安装kermit #apt-get install ckermit 2.使用kermit之前,在用户宿主目录下(/home/用户名/)创建一个名为.kermrc的配置文件,内容如下 : set l ...

  10. POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)

    Invitation Cards Time Limit: 8000MS   Memory Limit: 262144K Total Submissions: 16178   Accepted: 526 ...