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. mysql插入一张表里的数据到另一张表

    公司的一个项目,做报表--要关联的表结构比较多,最后决定把要用的数据集合到一张新表中,需要用到以下的sql语法......分享下: web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要 ...

  2. linux systemctl 指令 —— 阮一峰

    Systemd 指令 原文链接如果有效,请点击原文链接查看.原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.ht ...

  3. g高分屏DataGrid里面checkbox不显示的解决办法

  4. list— 把数组中的值赋给一组变量

    (PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 array list ( mixed $var1 [, mixed $... ] ) 像 array() 一样,这不 ...

  5. java-jpa-criteriaBuilder使用入门

    项目中使用jpa ,第一次见查询起来一脸蒙,这就去查下jpa查询的方式,和概念. jpa 元模型 criteria 查询 CriteriaBuilder 安全查询创建工厂 CriteriaQuery ...

  6. tree 命令【转】

    本文转载自:http://www.jb51.net/LINUXjishu/283874.html linux下怎么用tree命令以树形结构显示文件目录结构?tree命令可以以树形结构显示文件目录结构, ...

  7. linux crontab使用

    1.查看.编辑和删除 cron把命令行保存在crontab(cron table)文件里,这个文件通常在 /etc 目录下. 每个系统用户都可以有自己的crontab(在 /var/spool/cro ...

  8. linux上不能显示Jfreechart的图片文件

     出现错误: Jan 23, 2015 4:19:21 PM org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.s ...

  9. HRBUST 2072 树上求最大异或路径值

    一个很经典的套路 思想是 F [u,v] = F[1,u] ^ F[1,v] 这样就转化成了n个数两两异或 求最大值 可以用字典树来做 每次用当前数去树中寻求最大xor值 然后把这个数字插进去 就相当 ...

  10. QT Creator 代码自动补全

    QT Creator 代码自动补全 用QT Creater编程,如果没有自动补全是很痛苦的事情,于是便查阅了QT的文档,发现CTRL+SPACE是自 动补全的快捷键;但是在 Creater里使用居然没 ...