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?

解1 计数排序

 class Solution {
public:
void sortColors(vector<int>& v) {
vector<int> count(, ); for (int i = ; i < v.size(); ++i) {
if (v[i] == ) {
++count[];
} else if (v[i] == ) {
++count[];
} else {
++count[];
}
} int it = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < count[i]; ++j) {
v[it++] = i;
}
}
}
};

解2 双指针思想的延伸

 class Solution {
public:
void sortColors(vector<int>& v) {
int low = , mid = , high = v.size() - ;
while (mid <= high) {
if (v[mid] == ) {
swap(v[mid], v[low]);
++low;
++mid;
} else if (v[mid] == ) {
swap(v[mid], v[high]);
--high;
} else {
++mid;
}
}
}
};

解3 比较有技巧

 class Solution {
public:
void sortColors(vector<int>& v) {
int n0 = -, n1 = -, n2 = -; for (int i = ; i < v.size(); ++i) {
if (v[i] == ) {
v[++n2] = ;
v[++n1] = ;
v[++n0] = ;
} else if (v[i] == ) {
v[++n2] = ;
v[++n1] = ;
} else {
v[++n2] = ;
}
}
}
};

【LeetCode】075. Sort Colors的更多相关文章

  1. 【LeetCode】75. Sort Colors (3 solutions)

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

  2. 【LeetCode】75. Sort Colors 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...

  3. 【leetcode】75. Sort Colors

    题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...

  4. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  5. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  6. 【一天一道LeetCode】#75. Sort Colors

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  8. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

  9. 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...

随机推荐

  1. pyhton3 hashlib模块

    hashlib模块提供一下常量属性 hashlib.algorithms_guaranteed 获取保证在所有平台上此模块支持的哈希算法名称的集合 hashlib.algorithms_availab ...

  2. Scalability, Availability & Stability Patterns

    https://blog.csdn.net/ajian005/article/details/6191814   一 自我有要求的读者应该提出问题:(研习:掌握层次:)能力级别:不会(了解)——领会( ...

  3. 【HackerRank】 Filling Jars

    Animesh has N empty candy jars, numbered from 1 to N, with infinite capacity. He performs M operatio ...

  4. valn 配置

    内核修改: /device drivers/Network device support/MAC-VLAN support 1.创建目录和文件#cd /usr#mkdir vlan#cd vlan#c ...

  5. HDU 3449 Consumer

    这是一道依赖背包问题.背包问题通常的解法都是由0/1背包拓展过来的,这道也不例外.我最初想到的做法是,由于有依赖关系,先对附件做个DP,得到1-w的附件背包结果f[i]表示i花费得到的最大收益,然后把 ...

  6. log4j2.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!--日志级别以及优先级排序: OFF > FATAL ...

  7. Spring Cloud Stream消息总线

    Springcloud 里面对于MQ的整合一个是前一篇的消息总线一个是本文介绍的消息驱动 大体要学习这么几个知识点: 课题:SpringCloud消息驱动Stream1.什么是SpringCloud消 ...

  8. git checkout cannot stat permission denied

    https://stackoverflow.com/questions/5970879/git-rebase-error-cannot-stat-file-permission-denied 退出vi ...

  9. 【2039】checker(Dp)

    我今天脑子貌似又好使了一点,可以做一做DP中的水题了. 这个题难度蓝色,纯属是做的人太少了虚高. 这个题很显然的是可以用一个顺序一个逆序这两个大水转移方程轻松转移出到达这个地方最少需要的棋子数量,然后 ...

  10. 常用的python 库地址

    来源   https://pypi.python.org/pypi wordcloud  https://github.com/amueller/word_cloud.git jieba https: ...