Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

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 a one-pass algorithm using only constant space?
 

这道题的本质还是一道排序的题,题目中给出提示说可以用计数排序,需要遍历数组两遍,那么先来看这种方法,因为数组中只有三个不同的元素,所以实现起来很容易。

- 首先遍历一遍原数组,分别记录 0,1,2 的个数。
- 然后更新原数组,按个数分别赋上 0,1,2。

解法一:

class Solution {
public:
void sortColors(vector<int>& nums) {
vector<int> colors();
for (int num : nums) ++colors[num];
for (int i = , cur = ; i < ; ++i) {
for (int j = ; j < colors[i]; ++j) {
nums[cur++] = i;
}
}
}
};

题目中还要让只遍历一次数组来求解,那么就需要用双指针来做,分别从原数组的首尾往中心移动。

- 定义 red 指针指向开头位置,blue 指针指向末尾位置。

- 从头开始遍历原数组,如果遇到0,则交换该值和 red 指针指向的值,并将 red 指针后移一位。若遇到2,则交换该值和 blue 指针指向的值,并将 blue 指针前移一位。若遇到1,则继续遍历。

解法二:

class Solution {
public:
void sortColors(vector<int>& nums) {
int red = , blue = (int)nums.size() - ;
for (int i = ; i <= blue; ++i) {
if (nums[i] == ) {
swap(nums[i], nums[red++]);
} else if (nums[i] == ) {
swap(nums[i--], nums[blue--]);
}
}
}
};

当然我们也可以使用 while 循环的方式来写,那么就需要一个变量 cur 来记录当前遍历到的位置,参见代码如下:

解法三:

class Solution {
public:
void sortColors(vector<int>& nums) {
int left = , right = (int)nums.size() - , cur = ;
while (cur <= right) {
if (nums[cur] == ) {
swap(nums[cur++], nums[left++]);
} else if (nums[cur] == ) {
swap(nums[cur], nums[right--]);
} else {
++cur;
}
}
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/75

类似题目:

Sort List

Wiggle Sort II

Wiggle Sort

参考资料:

https://leetcode.com/problems/sort-colors/

https://leetcode.com/problems/sort-colors/discuss/26500/Four-different-solutions

https://leetcode.com/problems/sort-colors/discuss/26472/Share-my-at-most-two-pass-constant-space-10-line-solution

https://leetcode.com/problems/sort-colors/discuss/26760/C%2B%2B-solution-in-8-lines%3A-an-instance-of-the-Dutch-national-flag-problem-by-Edsger-Dijkstra

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 75. Sort Colors 颜色排序的更多相关文章

  1. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

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

  2. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  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. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  5. LeetCode 75. Sort Colors (颜色分类):三路快排

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  6. LeetCode 75 Sort Colors(颜色排序)

    翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...

  7. [LeetCode] Sort Colors 颜色排序

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

  8. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  9. Leetcode 75. Sort Colors

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

随机推荐

  1. STS 重写父类方法的操作

    本来这种东西真的没什么好写的,但是很多时候真的是要用到的,不知道的话自己手动敲,会累死人的.所以记录在这里,自己的笔记,有需要的赶紧拿去,省的手动录入那么辛苦. 在代码窗口点击右键,依次选择“Sour ...

  2. [线段树]Luogu P3372 线段树 1【模板】

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #d ...

  3. win10每次开机都会自检系统盘(非硬件故障)——解决方案2019.07.12

    1.最近反复遇到了这个问题,之前遇到这个问题就把系统重装了,没想到今天又遇到了,目前系统东西太多了,重装太麻烦了,就下决心解决一下. 2.不要使用网络上流传的修改注册表的方案,把注册表的那个键值删除那 ...

  4. IIS锁定是默认设置的 (overrideModeDefault="Deny")问题解决

    发布网站时提示错误 锁定是默认设置的 (overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 ...

  5. 一个人的公众号,我写了1w+

    大家好,我是Bypass,一个人一直保持着写博客的习惯,为此维护了一个技术公众号,致力于分享原创高质量干货,写的内容主要围绕:渗透测试.WAF绕过.代码审计.应急响应.企业安全. 一直以来,我把它当成 ...

  6. Neo4j 第九篇:查询数据(Match)

    Cypher使用match子句查询数据,是Cypher最基本的查询子句.在查询数据时,使用Match子句指定搜索的模式,这是从Neo4j数据库查询数据的最主要的方法.match子句之后通常会跟着whe ...

  7. Asp.Net真分页技术

    最近学校要做课题,闲来没事研究了下Asp.net的分页,我使用Repeater进行数据的绑定,每次从数据库读取到8条数据填充到Repeater中,这样搞可以降低数据库的压力,提高效率. 效果图如下: ...

  8. linux下sendmail

    1. 安装 # yum install sendmail.x86_64 bin/mail会默认使用本地sendmail发送邮件,这样要求本地的机器必须安装和启动sendmail服务,配置非常麻烦,而且 ...

  9. 配置全文搜索引擎solr

    前言 solr是apache下的一个子项目,用java编写基于Lucene开发的全文搜索服务器,不同于Lucene,solr一个完成的搜索服务器,提供了众多接口调用,而Lucene只是个工具包.如果用 ...

  10. Lumen5.7快速实现Captcha图片验证码功能

    公司发送短信注册的接口需要防刷,需要一个图形验证码,不考虑收费产品. Lumen5.7+nginx+mysql 使用了这个作者的扩展包,只讲实现.https://github.com/Youngyez ...