【LeetCode】75. Sort Colors(颜色排序)-C++实现的两种方法及超详细图解
问题描述:
一、第一种解题方法-计数排序法
(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++实现的两种方法及超详细图解相关推荐
- 【排序】LeetCode 75. Sort Colors
LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- [LeetCode]75.Sort Colors
[题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...
- python画图颜色填充_python画图的两种方法
python如何画图?这里给大家介绍两款python绘图的库:turtle和Matplotlib. 相关推荐:<python视频> 1 安装turtle Python2安装命令:pip i ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色.在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Given ...
- leetcode 75. Sort Colors | 75. 颜色分类(荷兰国旗问题,快速排序)
题目 https://leetcode.com/problems/sort-colors/ 题解 快速排序3.0(随机快排+荷兰国旗技巧优化) 在arr[L-R]范围上,进行快速排序的过程: 1)在这 ...
- LeetCode 75. Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 个人记录-LeetCode 75. Sort Colors
问题: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 数据结构六种常见的排序方法(超详细图解内附代码)
这里写目录标题 实验目的 实验内容 实验要求 六种排序方法细解 直接插入排序 冒泡排序 简单选择排序 希尔排序 快速排序 归并排序 六种排序好坏分析 代码段 运行结果 实验目的 1.能够清楚表述主要内 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
最新文章
- html页面多个按钮点击事件监听事件,HTML Button.onclick 事件汇总
- 云计算的认识和看法_云计算的个人理解与体会
- 开学季,如何用数据保证学生安全?这套方案值得一看
- 没有足够多的数据怎么办?计算机视觉数据增强方法总结
- 1040. 有几个PAT(25
- Centos 6.5 安装 Nginx+MySQL+PHP
- plc 上位机编译算法_什么是PLC与DDC PLC与DDC的区别
- 适配器模式之门面模式
- Java中银行转账的一些问题
- 分数加减法混合计算机,1649.新人教版五年级数学下册第三课 分数加减混合运算(教案)(教学设计)(国家级一等奖、适合公开课).doc...
- 用opencv压缩图片
- 高等数学:第八章 多元函数的微分法及其应用(6)微分法在几何上的应用
- tar命令打包并压缩指定的文件夹并且排除指定的文件
- Redux中的reducer到底是什么,以及它为什么叫reducer?
- Android 6.0 双向通话自动录音
- 导入的素材PS突然很卡,但是内存足够、素材图层也不多。。。该怎么办呢????
- 【转】Java技能清单
- 树莓派学习记录1-树莓派系统烧录与无屏幕网线连接开机
- 回文树/回文自动机 引入
- python中求最小公约数,python求最大公约数和最小公倍数的简单方法