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. struts1与strut2的区别

    struts1和struts2是两个完全不同的框架 struts1工作流程:发布Struts Web服务时,根据web.xml初始化ActionServlet,ActionContext等内容.在接到 ...

  2. Linux开机自动挂载Windows分区

    使用Linux的朋友肯定都不会对本文所谈的内容陌生,在Linux系统里,通常不会开机自动挂载Windows文件系统下的分区.Ubuntu系统下要点击Windows分区才会挂载,Fedora下则甚至要输 ...

  3. Command-line interface

    A command-line interface (CLI), also known as command-line user interface, console user interface, a ...

  4. 仿写自己的一个加载语言包的L函数

    <?php /** * [L 加载语言的L的方法] * @param [string] $key [语言键的名称] * @return [string] $value [取到的语言值] */ f ...

  5. VC++深入详解-第一章学习心得(二)

    前面介绍到了注册窗口,接下来进行窗口的创建,利用CreateWindow函数,结构如下 CreateWindowExW( DWORD dwExStyle, LPCWSTR lpClassName, / ...

  6. 我的第一个 Rails 站点:极简优雅的笔记工具-Raysnote

    出于公司开发需求,这个暑假我開始搞Ruby on Rails.在业余时间捣鼓了一个在线笔记应用:http://raysnote.com.这是一个极简而优雅的笔记站点(至少我个人这么觉得的). 笔记支持 ...

  7. js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)

    主要的代码是红色区域,HTML5获取本地文件对象并进行操作 //给上传按钮添加点击事件 $(".myappTXTUploadBtn").click(function(){ var ...

  8. window下手动搭建 PHP+Nginx+Mysql(转)

    首先还是下载 nginx:  http://nginx.org/en/download.html php  :  http://windows.php.net/download/ mysql:  ht ...

  9. 【Java基础】Jar包结构结构分析和操作具体解释

    作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 一 ...

  10. ARCGIS获取图层下的要素信息及要素类转换为要素

    /// <summary> /// 得到需要的字段名和字段值 /// </summary> /// <param name="layer">&l ...