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?

 
代码: one-pass algorithm
 class Solution {
public:
void sortColors(int A[], int n) {
int left=, right=n;
for(int i=; i<right; i++)
{
if(A[i]==)
{
A[i] = ;
A[left++] = ;
}
else if(A[i]==)
{
--right;
int temp = A[right];
A[right] = ;
A[i] = temp;
--i;
}
}
}
};

[LeetCode OJ] 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 so that objects of the same colo ...

  7. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  8. leetcode 【 Sort Colors 】python 实现

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

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

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

随机推荐

  1. HDU 1078 FatMouse and Cheese (记忆化搜索+dp)

    详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...

  2. .net 码农转战 iOS - 初探

    好久没写博客了,之前还打算把毕业设计中涉及到的两个算法拿出来说说(脸型分析 + 声音分析),博文都写了一半了,后来实在太忙了,那篇随笔也就沉在草稿列表中没动过. 我原先是专职 .net 开发的,在公司 ...

  3. Centos6.x X64 飞信安装

    Centos6.x X64 飞信安装 1,安装飞信依赖包 yum -y install glibc.i686 krb5-libs.i686 libstdc++.i686 zlib.i686 --set ...

  4. canvas 俄罗斯方块

    <!doctype html> <html> <body> <canvas id="can" width="360px" ...

  5. windows2012 IIS8.5 不能在此路径中使用此配置节

    IIS 8.5 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 o ...

  6. CSharp - Comparison between IComparer and IComparable

    /* Author: Jiangong SUN */ I've already written an article introducing the usage of comparer here. I ...

  7. Android 获取WIFI MAC地址的方法

    1. 经常用法,调用Android的API:WifiManager <uses-permission android:name="android.permission.ACCESS_W ...

  8. OpenGL ES2学习笔记(9)-- 转换矩阵

    线性代数是计算机图形学的一块基石,本篇文章总结如何在Shader中使用矩阵来移动.缩放和旋转顶点. 代码和效果 把下面的代码复制到OpenGL Console里: import java.nio.By ...

  9. MySQL强化

    大纲: 数据约束 数据库设计(表设计) 关联查询(多表查询) 存储过程 触发器 mysql权限问题 1 数据约束 1.1 什么是数据约束 对用户操作表的数据进行约束. 1.2 约束种类 1.2.1 默 ...

  10. Linux下搭建Oracle11g RAC(1)----IP分配与配置IP

    首先需要说明的,我的RAC搭建不是在虚拟机上完成的,而是在实际部署中,二者之间有些许差异,本人水平有限,请见谅. 其中,每台机器至少需要配置3个IP地址,在安装操作系统的过程中,我们需要配置公网IP和 ...