问题描述:

一、第一种解题方法-计数排序法 

(1)分别统计0,1,2的元素个数

        int count[3] = {0};    // 存放0, 1, 2三个元素的频率for(int i = 0 ; i < nums.size() ; i ++){assert(nums[i] >= 0 && nums[i] <= 2);count[nums[i]] ++;}

注意:assert(nums[i] >= 0 && nums[i] <= 2),养成判断错误的习惯

(2) 然后,重新放回到到数组中

        int index = 0;for(int i = 0 ; i < count[0] ; i ++)nums[index++] = 0;for(int i = 0 ; i < count[1] ; i ++)nums[index++] = 1;for(int i = 0 ; i < count[2] ; i ++)nums[index++] = 2;

(3)第一种解法完整实现代码:

#include <cassert>using namespace std;// 75. Sort Colors
// https://leetcode.com/problems/sort-colors/description/
//
// 计数排序的思路
// 对整个数组遍历了两遍
// 时间复杂度: O(n)
// 空间复杂度: O(k), k为元素的取值范围
class Solution {
public:void sortColors(vector<int> &nums) {int count[3] = {0};    // 存放0, 1, 2三个元素的频率for(int i = 0 ; i < nums.size() ; i ++){assert(nums[i] >= 0 && nums[i] <= 2);count[nums[i]] ++;}int index = 0;for(int i = 0 ; i < count[0] ; i ++)nums[index++] = 0;for(int i = 0 ; i < count[1] ; i ++)nums[index++] = 1;for(int i = 0 ; i < count[2] ; i ++)nums[index++] = 2;}
};int main() {int nums[] = {2, 2, 2, 1, 1, 0};vector<int> vec = vector<int>(nums, nums + sizeof(nums)/sizeof(int));Solution().sortColors(vec);for(int i = 0 ; i < vec.size() ; i ++)cout << vec[i] << " ";cout << endl;return 0;
}

二、第二种解题方法-三路快速排序法(Quick Sort 3 Ways)

(1)先用动画演示解题思路

①将数组分成3个部分

②回到本题,我们需要对0,1,2进行排序

 ③将数组按元素0,1,2分成三个部分

④ 使用下标 i 作为遍历,此时arr[ i ] = e

 若e = 1,则arr[ i ] = e; 然后 i++

⑤若arr[ i ] = e, e = 2;则two--;swap( nums[i] , nums[two])

⑥最后完成数组的排序

 (2)第二种解题方法的实现代码

// 三路快速排序的思想
// 对整个数组只遍历了一遍
// 时间复杂度: O(n)
// 空间复杂度: O(1)
class Solution {
public:void sortColors(vector<int> &nums) {int zero = -1;          // [0...zero] == 0int two = nums.size();  // [two...n-1] == 2for(int i = 0 ; i < two ; ){if(nums[i] == 1)i ++;else if (nums[i] == 2){--two;swap( nums[i] , nums[two]);}else{ // nums[i] == 0assert(nums[i] == 0);++zero;swap(nums[zero] , nums[i]);i++;}}}
};int main() {int nums[] = {2, 2, 2, 1, 1, 0};vector<int> vec = vector<int>(nums, nums + sizeof(nums)/sizeof(int));Solution().sortColors(vec);for(int i = 0 ; i < vec.size() ; i ++)cout << vec[i] << " ";cout << endl;return 0;
}

参考资料:

https://coding.imooc.com/class/chapter/82.html#Anchor

【LeetCode】75. Sort Colors(颜色排序)-C++实现的两种方法及超详细图解相关推荐

  1. 【排序】LeetCode 75. Sort Colors

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

  2. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  3. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

  4. python画图颜色填充_python画图的两种方法

    python如何画图?这里给大家介绍两款python绘图的库:turtle和Matplotlib. 相关推荐:<python视频> 1 安装turtle Python2安装命令:pip i ...

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

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

  6. leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)

    题目 https://leetcode.com/problems/sort-colors/ 题解 快速排序3.0(随机快排+荷兰国旗技巧优化) 在arr[L-R]范围上,进行快速排序的过程: 1)在这 ...

  7. LeetCode 75. Sort Colors

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

  8. 个人记录-LeetCode 75. Sort Colors

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

  9. 数据结构六种常见的排序方法(超详细图解内附代码)

    这里写目录标题 实验目的 实验内容 实验要求 六种排序方法细解 直接插入排序 冒泡排序 简单选择排序 希尔排序 快速排序 归并排序 六种排序好坏分析 代码段 运行结果 实验目的 1.能够清楚表述主要内 ...

  10. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

最新文章

  1. html页面多个按钮点击事件监听事件,HTML Button.onclick 事件汇总
  2. 云计算的认识和看法_云计算的个人理解与体会
  3. 开学季,如何用数据保证学生安全?这套方案值得一看
  4. 没有足够多的数据怎么办?计算机视觉数据增强方法总结
  5. 1040. 有几个PAT(25
  6. Centos 6.5 安装 Nginx+MySQL+PHP
  7. plc 上位机编译算法_什么是PLC与DDC PLC与DDC的区别
  8. 适配器模式之门面模式
  9. Java中银行转账的一些问题
  10. 分数加减法混合计算机,1649.新人教版五年级数学下册第三课 分数加减混合运算(教案)(教学设计)(国家级一等奖、适合公开课).doc...
  11. 用opencv压缩图片
  12. 高等数学:第八章 多元函数的微分法及其应用(6)微分法在几何上的应用
  13. tar命令打包并压缩指定的文件夹并且排除指定的文件
  14. Redux中的reducer到底是什么,以及它为什么叫reducer?
  15. Android 6.0 双向通话自动录音
  16. 导入的素材PS突然很卡,但是内存足够、素材图层也不多。。。该怎么办呢????
  17. 【转】Java技能清单
  18. 树莓派学习记录1-树莓派系统烧录与无屏幕网线连接开机
  19. 回文树/回文自动机 引入
  20. python中求最小公约数,python求最大公约数和最小公倍数的简单方法

热门文章

  1. union并不绝对比or的执行效率高
  2. 主要几个浏览器的内核是什么
  3. python常用颜色表示_Python命令行的常用颜色,python,字体
  4. linux挂载移动硬盘 格式化_Linux下挂载移动硬盘(NTFS格式)
  5. 普通话测试-短文60篇文章,附带拼音(1-10篇)
  6. C++ Primer 第三版电子版PDF
  7. PM Interview 60quiz
  8. 9.2 多元微分学及应用——偏导数
  9. 2012考研数学二第(11)题——链式法则偏导数
  10. java导出出行客人到Excel