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?

Summary:  Accepted at the first submission. Nice.

     void sortColors(int A[], int n) {
int red_idx = -;
int blue_idx = n;
for(int i = ; i < n; i ++) {
if(i == blue_idx)
break;
if(A[i] == ){
int tmp = A[i];
A[i] = A[++red_idx];
A[red_idx] = tmp;
} if(A[i] == ) {
int tmp = A[i];
A[i] = A[-- blue_idx];
A[blue_idx] = tmp;
i--;
}
}
}

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

  1. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  2. Sort Colors —— LeetCode

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

  3. Sort Colors leetcode java

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

  4. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  5. [Leetcode Week2]Sort Colors

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

  6. 【LeetCode】Sort Colors 数组排序

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

  7. LeetCode 75. Sort Colors (颜色分类):三路快排

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

  8. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  9. 【LeetCode】Sort Colors

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

随机推荐

  1. Hello Spring Framework——面向切面编程(AOP)

    本文主要参考了Spring官方文档第10章以及第11章和第40章的部分内容.如果要我总结Spring AOP的作用,不妨借鉴文档里的一段话:One of the key components of S ...

  2. Django进阶2

    一.ORM操作进阶 ForeignKey关联 示例models from django.db import models # Create your models here. class User(m ...

  3. INNO SETUP 读取可变注册表路径的问题

    ;INNO 读取可变注册表路径的问题 ;问题:;我想自动为 FireFox 安装上 Real 的 Mozilla 插件~但是它的路径存放在"HKEY_CURRENT_USER\Softwar ...

  4. SQL Server2014,附加数据库失败,错误为:5120的解决方法

    在SQL Server 2014附加数据库的时候,报错为: 无法打开物理文件XXX,操作系统错误5(拒绝访问),SQL Server 错误5120 解决方法:  我的电脑→管理→服务和应用程序→ 服务 ...

  5. angularjs $broadcast 和 $on 的使用及其注意事项

    下面是demo: <div ng-controller="ParentCtrl"> www.111cn.net //父级 <div ng-controller=& ...

  6. php读取大文件

    高效率计算文件行数 function count_line($file) { $fp=fopen($file, "r"); $i=0; while(!feof($fp)) { // ...

  7. CSS样式表(二)

    [layout] clear:该属性的值指出了不允许有浮动对象的边. 默认值:none none: 允许两边都可以有浮动对象 both: 不允许有浮动对象 left: 不允许左边有浮动对象 right ...

  8. C#序列化及反序列化Json对象通用类JsonHelper

    当今的程序界Json大行其道.因为Json对象具有简短高效等优势,广受广大C#码农喜爱.这里发一个序列化及反序列化Json对象通用类库,希望对大家有用. public class JsonHelper ...

  9. web前端面试题总结

    HTML Doctype作用? 严格模式与混杂模式如何区分?它们有何意义? (1).<!DOCTYPE> 声明位于文档中的最前面,处于 <html> 标签之前.告知浏览器的解析 ...

  10. 基于EasyUI的Web应用程序及过去一年的总结

    前言 在这家公司服务了一年时间,一个多月之前已经提交了离职申请,好在领导都已经批准了,过几天就办理手续了,在此感谢领导的栽培与挽留,感谢各位同事在工作中的给我的帮助,感谢师傅(在我心中当他是我师傅,跟 ...