leetcode 75 Sort Colors 计数排序,三路快排


解法一:计数排序:统计0,1,2 的个数
时间复杂度:O(n)
空间复杂度:O(k) k为元素的取值范围, 此题为O(1)
class Solution {
public:
void sortColors(vector<int>& nums) {
int count[] = {}; //存放0,1,2三个元素的频率
for(int i=;i<nums.size();i++){
assert(nums[i] >= && nums[i]<=); //若不符合条件则报错
count[nums[i]] ++;
}
int index = ;
for(int i=;i<count[];i++)
nums[index++] = ;
for(int i=;i<count[];i++)
nums[index++] = ;
for(int i=;i<count[];i++)
nums[index++] = ;
}
};
解法二:三路快排

时间复杂度:O(n)
空间复杂度:O(1)
只遍历了一遍
class Solution {
public:
void sortColors(vector<int>& nums) {
int zero = -; //nums[0...zero] == 0 , 即设置初始状态为无效的数组
int two = nums.size(); //nums[two...n-1] ==2, 初始化two==n
for(int i=;i<two;){
//有一部分i不需要++
if(nums[i] == )
i++;
else if(nums[i] ==){
two -- ; //two移位到前一位,即还没有处理的元素上
//将two前的还未排序的元素与2交换位置
swap(nums[i],nums[two]);
}
else{
//nums[i] == 0
assert(nums[i] ==);
zero ++ ;
//zero++后指向的是1,相当于将1和0交换位置,所以i++
swap(nums[zero],nums[i]);
i++;
}
}
}
};
leetcode 75 Sort Colors 计数排序,三路快排的更多相关文章
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys
很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...
- LeetCode 75 Sort Colors(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
随机推荐
- Ubuntu16 install Anaconda
gbt@gbt-Precision-7720:~$ gbt@gbt-Precision-7720:~$ cd Anacondagbt@gbt-Precision-7720:~/Anaconda$ gb ...
- BeanUtils简单应用
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...
- PyGrub
from:https://wiki.debian.org/PyGrub Using pyGRUB on Wheezy to boot a domU kernel Using pyGRUB from x ...
- virsh 查看hypervisor特性
[root@opennebula var]# virsh -c qemu:///system nodeinfo CPU model: x86_64 CPU(s): CPU frequency: MHz ...
- TTS技术
一.简介 TTS技术,TTS是Text To Speech的缩写,即"从文本到语音".它将计算机自己产生的.或外部输入的文字信息转变为可以听得懂的.流利的汉语口语(或者其他语言语音 ...
- Bean管理注解的例子
- ABP源码uml类图
陆陆续续学习ABP框架有一段时间了,阳光铭睿的入门教程和HK Zhang的源码分析文章对我的学习帮助都很大.之所以会花这么大工夫去学习ABP.看ABP的源代码,一是因为本人对于DDD也非常有兴趣,AB ...
- Android下拉选择框之PopupWindow
1.效果图 2.思路分析 1.点击弹出对话框 popupwindow 2.对popupwindow进行相关设置,popupwindow中设置view为listview 3.listview中item设 ...
- attachEvent 与 addEventListener 的监听
说到 attachEvent 与 addEventListener 的事件必然会提到 浏览器的判断,因为attachEvent只适用于于IE 先来看看常用的浏览器的判断 //判断浏览器类型 if(n ...
- WinForm中DataGridView的使用(五) - 自定义列
DataGridView支持指定DataGridViewImageColumn.DataGridViewButtonColumn等特殊类型的列,加入到Columns中. DataGridViewIma ...