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 ...
随机推荐
- css四可见,部分可见和重叠半透明
<html> <head> <title>javascript</title> <style type="text/css"& ...
- ObjectMapper对象的使用 Object2JSON
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler ...
- Python基础-5
目录 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 hashlib re正则表达式 模块 ...
- UltraEdit 回车符替换空格
查找和替换 输入 ^r^n 替换为:(空格)
- IOS 防坑指南
1. 读写文件 1. IOS 8 中 stringWithContentsOfFile 已被移除 2. 创建文件必须放到 应用下 Documents 下面 // // FileHelper.swif ...
- NPOI 2.1.3.1导入Excel
引入NPOI 2.1.3.1的包 项目引入 using NPOI.XSSF.UserModel;using NPOI.SS.UserModel; 控制器方法: public ActionResult ...
- WPF的Image控件图片不能显示出来的问题探究
在wpf项目中,用Image来显示资源图片,在界面是可以显示,但是在运行的时候却显示不出来. <Image Source=" HorizontalAlignment="Lef ...
- 【leetcode 138. 复制带随机指针的链表】解题报告
方法一:递归 unordered_map<Node*,Node*> dict; Node* copyRandomList(Node* head) { if (!head) return h ...
- Apple导出p12证书 导出证书为p12 Apple开发
1.原因说明 p12证书包含了我们的cer证书和私钥 这个证书可以当做我们开发凭证的备份 在我们更换开发机器的时候不需要再去Apple开发中心申请了 2.导出过程 2.1 打开钥匙串访问 2.2 选择 ...
- 主流C语言编译器介绍