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. tomcat部署https+TLS 1.2+Apple ATS支持

    因为苹果ATS的要求, tomcat服务器要求上https+TLS1.2, 前面搞定了https,但是tls一直是1.0, 甚至把跑了一年的服务器重启了, 不解决问题. 思路如下: 1. 将openJ ...

  2. presto的动态化应用(一):presto节点的横向扩展与伸缩

    一.presto动态化概述 近年来,基于hadoop的sql框架层出不穷,presto也是其中的一员.从2012年发展至今,依然保持年轻的活力(版本迭代依然很快),presto的相关介绍,我们就不赘述 ...

  3. [已解决] MAVEN安装代码到本地库,安装jar, source, javadoc的方式

    mvn install:install-file -Dfile=a.jar -DgroupId=gid -DartifactId=aid -Dversion=0.0.1 -Dpackaging=jar ...

  4. Kafka深度解析

    本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...

  5. mysql 日志表rename 备份

    1. 按照原历史表新增一个新表(空表): mysql> create table history_log_new ...; 2. 给历史表重命名,并将新表重命名为历史表: mysql> R ...

  6. 使用ajax分页

    前台页面: <table class="table table-hover"> <thead> <tr> <th class='hidde ...

  7. jquery.Huploadify 上传

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  8. css样式增加&改变颜色

    .listyle{ color:blue; background-color:red /* background-color:"#006633" */ } .intro{ font ...

  9. (转)解决Mac OS X上PhpStorm不能输入中文

    看到Netbeans上类似问题的解决办法: /Applications/netbeans/NetBeans 6.7.1/Content/Resource/netbeans/etc/netbeans.c ...

  10. hibernate联合主键 注解方式

    转载自https://my.oschina.net/yotoo/blog/265571 方法一:主键类用@Embeddable,pojo类仍然用@Entity但是引用主键类的对象用@Id 主键pojo ...