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?

  于链表的双指针问题,这里也要track两个index,一个是red的index,一个是blue的index,两边往中间走。

class Solution {
public:
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int zeroPos = ;
int twoPos = n-;
int i = ;
while(i<= twoPos){
int temp;
if(A[i] == ){
temp = A[zeroPos];
A[zeroPos] = ;
A[i] = temp;
i++;zeroPos++;
}else if( == A[i]){
temp = A[twoPos];
A[twoPos] = ;
A[i] = temp;
twoPos--;
}else
i++;
}
}
};

LeetCode_Sort Colors的更多相关文章

  1. Sort Colors

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

  2. [LeetCode] Sort Colors 颜色排序

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

  3. Leetcode 75. Sort Colors

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

  4. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

  6. Sort Colors [LeetCode]

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

  7. 【LeetCode】Sort Colors

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

  8. PAT (Advanced Level) Practise:1027. Colors in Mars

    [题目链接] People in Mars represent the colors in their computers in a similar way as the Earth people. ...

  9. LintCode Sort Colors

    For this problem we need to sort the array into three parts namely with three numbers standing for t ...

随机推荐

  1. LeetCode_Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  2. windows系统各版本 各种数据结构

    极爽啊http://msdn.moonsols.com/

  3. bzoj2325 [ZJOI2011]道馆之战

    Description 口袋妖怪(又名神奇宝贝或宠物小精灵)红/蓝/绿宝石中的水系道馆需要经过三个冰地才能到达馆主的面前,冰地中的每一个冰块都只能经过一次.当一个冰地上的所有冰块都被经过之后,到下一个 ...

  4. hdu2082:简单母函数

    题目大意: a,b,c,d...z这些字母的价值是1,2,3......26 给定 这26个字母分别的数量,求总价值不超过50的单词的数量 分析: 标准做法是构造母函数 把某个单词看作是,关于x的多项 ...

  5. usaco5.5-Hidden Passwords

    最小表示法,感觉可以做成个模板,第一次RE是因为字符串长度变2倍了而我把数组开小了 Executing...   Test 1: TEST OK [0.008 secs, 3760 KB]   Tes ...

  6. 充分发挥 JavaScript 语言的优势

    尽管我在生产环境中使用 JavaScript 长达 8 年之久了,但是,直到最近 2 年,我才开始学习如何正确地编写 JavaScript 代码,根据我对人们的理解,很多开发者都有类似经历.我们有相当 ...

  7. 深入了解Json转变为map的思想,附源代码2

    最近在做一个投票情况的用例,返回的结果打算放到JSON中 数据库的结果集如上图所示:optionkey代表选项,optionval代表其值 第一次做的时候考虑应该键值对应的关系,所以前台接到的json ...

  8. 页面onclick()中传值问题

    html中onclick()里面传变量到javascript中的问题,小小的记录下: 传变量的话一定要加  '' <span onclick="sellGoods('${session ...

  9. PyCharm常用设置

    pycharm,优秀的python开发工具 本文介绍一点python开发工具,pycharm的使用方式. 内容仅仅为最常用的几点,想要了解更多,请自行谷歌. 1.常用工具栏 唤出常用工具栏,View ...

  10. C# 大小写转换

    全部大写: string upper = str.ToUpper() 全部小写: string lower = str.ToLower(); str是需要转换的字符.