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. 疯狂delphi - 朱建强 (一些小例子很实用,也是我所关心的几个问题)

    疯狂delphi - 朱建强 (一些小例子很实用,也是我所关心的几个问题) Android实例-获取安卓手机WIFI信息(XE8+小米2)http://www.cnblogs.com/FKdelphi ...

  2. 编译Qt 4.7.3的时候发生NMAKE : fatal error U1077: 'cd' : return code '0x2'

    怀疑是configure的时候没加-nomake demos -nomake examples的问题 references: http://stackoverflow.com/questions/10 ...

  3. HP的笔记本经常蓝屏崩溃 -------athr.sys

    因为windows 7才新装不久,没有时间下载配置什么符号表,直接临时下载了WinDbg分析下Dump文件, Probably caused by : athr.sys ( athr+428a5 ) ...

  4. AzCopy – 上传/下载 Windows Azure Blob 文件

    在我们收到的请求中,有一个频繁出现的请求是提供一种能在 Windows Azure Blob 存储与其本地文件系统之间轻松上传或下载文件的方法.一年半前, 我们很高兴地发布了 AzCopy, Wind ...

  5. 2015第24周一Spring事务

    1. Spring事务管理简介 (1)Spring为多种不同类型的事务管理机制提供统一编程模型,这些事务管理模型包括JTA.JDBC.Hibernate.JPA和JDO. (2)Spring支持声明式 ...

  6. poj1284:欧拉函数+原根

    何为原根?由费马小定理可知 如果a于p互质 则有a^(p-1)≡1(mod p)对于任意的a是不是一定要到p-1次幂才会出现上述情况呢?显然不是,当第一次出现a^k≡1(mod p)时, 记为ep(a ...

  7. 使用layer显示弹出框笔记

    $.layer({     area : ['200px','auto'], //控制层宽高.当设置为auto时,意味着采用自适应, 当然,对于宽度,并不推荐这样做.例如:area : ['310px ...

  8. 用Scrapy写一个爬虫

    昨天用python谢了一个简单爬虫,抓取页面图片: 但实际用到的爬虫需要处理很多复杂的环境,也需要更加的智能,重复发明轮子的事情不能干, 再说python向来以爬虫作为其擅长的一个领域,想必有许多成熟 ...

  9. javascript对URL中的参数进行简单加密处理

    javascript的api本来就支持Base64,因此我们可以很方便的来进行编码和解码. var encodeData = window.btoa("name=xiaoming&a ...

  10. struts配置,略记

    <!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener ...