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. Dynamics AX 2012 R2 业务系列-采购业务流程

    在博文Dynamics AX R2 业务系列中,Reinhard对这个系列做了一个规划,下面我们就按照规划开始说业务吧. 国际惯例,从采购开始. 1.采购的主要职责 简单点说,采购的主要职责,是从供应 ...

  2. 反射 __import__

    __import__ 根据字符串导入模块 def run(): inp = input('请输入URL:') m,p = inp.split('/') obj = __import__(m) if h ...

  3. easyui dialog iframe

    function toGrant(obj,url,showMsg) {                                        var dialog=$('#dlg_grant' ...

  4. python 学习随笔

    要求: 用户输入一个数,比如输入387. 需要把387打乱,然后将这三个数组合成最大数输出. 如果最大值和用户输入的等大,输出-1. 方法一: #!/user/bin/env python # _*_ ...

  5. ipad pro 文章

    这篇文章是通过iPad Pro发送的.体验一下键盘输入,以及safari下的输入.这个键盘的输入手感好一般,按键行程较短.

  6. modprobe和lsmod命令配合使用

    modprobe命令用于智能地向内核中加载模块或者从内核中移除模块. modprobe可载入指定的个别模块,或是载入一组相依的模块.modprobe会根据depmod所产生的相依关系,决定要载入哪些模 ...

  7. R语言基本操作函数---变量的基本操作

    1.变量变换        as.array(x),as.data.frame(x),as.numeric(x),as.logical(x),as.complex(x),as.character(x) ...

  8. HDFS体系架构

    Master-slaver结构,namenode是中心服务器维护着文件系统树和整个树内的文件目录, 负责整个数据集群的管理.datanode分布在不同的机架上,在客户端和namenode的调度下 存储 ...

  9. JDBC三层架构

    三层框架: 通常意义上的三层架构就是将整个业务应用划分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL).区分层次的目的即为了“高内聚,低耦合”的思想. 原理:1:数据访问层:主要是对原始 ...

  10. Swift_1基础

    // swift中导入类库使用import,不再使用<>和""import Foundation // 输出print("Hello, World!" ...