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 ...
随机推荐
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- SSL认证
SSL认证 单向认证 1.发一串消息个对方 2.对方用私钥加密后返回 3.本方用对方的公钥解密,验证消息是否正确, 如果消息相同,则本方认可对方 双向认证 本方认证对方 对方认证本方
- Docker学习笔记_安装和使用mysql
一.系统环境和准备 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu18.04 3.操作账号:docker 二.安装 1.搜索mysql镜像:docker search mysql 2 ...
- TensorFlow中文手册
注意:本文只为读书笔记. 第一章 起步 - 起步 - [介绍](SOURCE/get_started/introduction.md) - [下载及安装](SOURCE/get_started/os_ ...
- delphi 金额大小写转换函数
{*------------------------------------------------ 金额大小写转换函数 @author 王云盼 @version V1506.01 在delphi7测 ...
- wp7启动+幻灯片效果
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Wi ...
- strcmp返回值布尔类型的判断
strcmp: 用于比较两个字符串,原型如下: int strcmp ( char const *s1, char const *s2):如果s1小于s2,strcmp函数返回一个小于零的值.如果s1 ...
- 个人常用Markdow语法代码备用
1.分隔线 -------------------------------- 2.OC代码 ``` Objective-C ``` 3.字体加粗 ##加粗## 4.标题样式 <h1> &l ...
- Mysql初识数据库《四》mysql安装与基本管理
一.MySQL介绍 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下公司.MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是 ...
- 一大波趣图:CSS的力量
CSS的力量 CSS的作用,一目了然~ 见识一下CSS的厉害! 用了CSS,效果显著 HTML5 + CSS3 + Javascript会怎么样? HTML ...