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.

click to show follow up.

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?

 class Solution {
public:
void swap(int &a, int &b){
int temp = a;
a = b;
b = temp;
}
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (n<) return; int first = ;
int last = n-;
while(first<n && ==A[first])
first++;
while(last>= && ==A[last])
last--; int i=first;
while(first < last && i<=last){
if (==A[i]){
swap(A[i],A[last]);
last--;
while(last>= && ==A[last])
last--;
}else if(==A[i]){
swap(A[i],A[first]);
first++;
if(i<first) i=first;
while(first<n && ==A[first])
first++;
}else{
i++;
}
}
}
};

我的答案

思路:如果只需要一次遍历,就地排序,其实在循环到每个数的时候要比两次遍历多做一些事情。因为0肯定是排在前面的,2肯定是排在后面的,指定两个指针,分别指向数组的两头,然后往中间缩进,注意分别讨论遍历到0,1,2时应该做何种运算,何种操作即可。

[leetcode.com]算法题目 - Sort Colors的更多相关文章

  1. LeetCode 75. 颜色分类(Sort Colors) 30

    75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...

  2. LeetCode(75) Sort Colors

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

  3. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  6. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  7. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  8. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. 什么时候出现死锁,如何解决?mysql 引擎? 多个like or 查询sql如何优化?什么是常量池?for条件执行顺序

    1. 什么时候出现死锁,如何解决?mysql 引擎? 多个like or 查询sql如何优化? 资源竞争导致死锁,比如A B 同时操作两条记录,并等待对方释放锁. 优化sql, 加缓存,主从(如读写分 ...

  2. 如何使用Log4j

    如何使用Log4j? 1. Log4j是什么?   Log4j可以帮助调试(有时候debug是发挥不了作 用的)和分析,要下载和了解更详细的内容,还是访问其官方网站吧: http://jakarta. ...

  3. inet_pton和inet_ntop inet_ntoa

    Linux下地址转换函数 inet_pton ==> Ox inet_ntop ==> xxx.xxx.xxx #include <sys/types.h> #include ...

  4. centos7 hbase 搭建笔记

    1.require:java环境,本地可用的hadoop 2.拷贝hbase文件(hive-1.2.6) 3.设置环境变量 export HBASE_HOME=/data/spark/bin/hbas ...

  5. ORACLE rollup函数

    rollup函数应用场景: 主要使用在 分组中,将每个分组求汇总值(就是小计),最后再讲所有值(除去小计)求和(就是合计) 当然,使用union 也可以达到同样的效果.先将需要查询的分组查出来,再un ...

  6. list集合去除重复对象的实现

    下面小编就为大家带来一篇list集合去除重复对象的实现.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 对象重复是指对象里面的变量的值都相等,并不定是地址.list集合存 ...

  7. 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)

    传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...

  8. 2018.10.30 NOIP模拟 排列树(树形dp+组合数学)

    传送门 考试的时候乱搞过了. 其实题目就是让你求拓扑排序方案数. 直接树形dpdpdp然后组合数转移一下就行了. 乱搞代码

  9. Vue + Element UI 实现权限管理系统

    Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html

  10. mysql-libs版本冲突卸载不了

    问题: 卸载mysql-libs时候出现如下依赖性无法卸载 [root@mail yum.repos.d]# rpm -e mysql-libserror: Failed dependencies: ...