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. Django(三)

    1.Django请求的生命周期         路由系统 -> 视图函数(获取模板+数据-->渲染) -> 字符串返回给用户   2.路由系统         /index/    ...

  2. animate动画jquery

    <script> $(".change").animate({height:"hide",width:"300px"},&quo ...

  3. 简明外贸报价单(Price List)范本

    简明外贸报价单(Price List)范本 简明外贸报价单(Price List)范本 报价单 Price List 报价日期:年 月   日 Supplier Address 供应商 公司地址 Co ...

  4. 机器人操作臂运动学入门一--D-H参数标定

    最近重新学习机器人方面的知识,想到一年以前在学校选修<机器人学技术基础>这门课的时候,老师虽然讲机器人的各个方面的知识都讲到了,但只是浮光绿影的的提到,并没有真正讲到深处,我的理解也没有更 ...

  5. python中文乱码问题

    在学习python的时候,当我要print中文的时候,会出现以下提示: py = '你好,世界!'print py File "n2.py", line 1 SyntaxError ...

  6. T检验与F检验的区别_f检验和t检验的关系

    1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错的概率,我们会利用统计学家所开发的一些统计方法,进行统计检定. 通过把所得到的统计检定值,与统计学家建立了一 ...

  7. mysql实战之 批量update

    mysql实战之批量update 现阶段我们的业务量很小,要对admin_user表中的relationship字段进行更新,指定id是409.已知409是公司内的一服务中心,需要把该服务中心放到区代 ...

  8. Command调用存储过程小实例

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  9. [Python]简易terminal歌词滚动播放器

    整合了网易云的一些API,想写一个terminal版的音乐播放器,但是还没有想好写成什么样子. 暂时写了一个必须又的功能:带歌词滚动的播放器,用了pygame里的mixer来播放音乐. 准备有时间的时 ...

  10. [原创]DC-DC输出端加电压会烧毁

      在调试智能钥匙连续开锁出现故障的问题排查过程中,为了对比模拟开关TS5A3166对于开锁数据通信的影响,尝试短接模拟开关的输入输出脚,未曾想乌龙了一把,错把DC-DC芯片输入输出短接了(两者都是S ...