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. iOS屏幕旋转

    三种方法 需求:全局主要是竖屏 个别界面需要横屏

  2. 怎么用AJAX来判断dedecms用户是否登录呢

    JS代码:Copy code<script language="javascript" src="{dede:global name='cfg_cmspath'/} ...

  3. Xcode 如何删除过期的Provisioning Profile文件

    Xcode 中所有的Provisioning Profile文件,都在 ~/Library/MobileDevice/Provisioning Profiles  这个文件夹下:进入该文件夹,按照文件 ...

  4. php webservice

    发请求客户端client.php <?php //需要到php.ini文件中打开extension=php_soap.dll try{ //wsdl方式调用web service //wsdl方 ...

  5. java编程实现日历

    package com.beiwo.other;/* * 需求:输入一个年份和月份 ,显示当前月日情况 ,星期数要对应准确 * 1.1900年1月1号开始 * 2.星期 : 直接用总天数对7求余数 3 ...

  6. Promise对象

    1.Promise思想:每一个异步任务立刻返回一个Promise对象,由于是立刻返回,所以可以采用同步操作的流程.这个Promises对象有一个then方法,允许指定回调函数,在异步任务完成后调用. ...

  7. NetBean常用快捷键(MAC中)

    shift+cmd+i:导入包 shift+alt+上:复制当前行,鼠标留在上一行   shift+alt+下:复制当前行,鼠标留在下一行 shift+ctrl+上:将选中行向上移动    shift ...

  8. jquey与javascript相通运用查找(全)

    1.addClass\removeClass\classList(js) jQ:$('#div').addClass('hover')=====document.getElementById('div ...

  9. Spring获取bean的工具类

    package com.tech.jin.util; import org.springframework.context.ApplicationContext; import org.springf ...

  10. Git使用指南(3)—— 使用Git命令

    暂存区替换掉工作区 git init git init newrepo 克隆仓库 git clone git clone <repo> git clone <repo> < ...