[刷题] 75 Sort Colors
要求
- 给只有0 1 2三个元素的数组排序
思路
- 方法1:遍历数组,利用辅助数组保存三个元素的个数,再写入(遍历两遍)
- 辅助数组有三个元素,对应0 1 2的个数
- 方法2:模拟三路快排,遍历一遍完成排序
- 三个索引,zero和two用于首尾的扩充,i用于遍历
- 遍历数组,每个元素只有三种可能
- 0 2移到首尾,1不动

实现
方法1
1 void sortColors(vector<int>& nums){
2 int count[3] = {0};
3 for( int i = 0 ; i < nums.size() ; i ++ ){
4 assert( num[i] >= 0 && nums[i] <= 2 );
5 count[nums[i]] ++;
6 }
7
8 int index = 0;
9 for( int i = 0 ; i < count[0] ; i ++ ){
10 nums[index++] = 0;
11 for( int i = 0 ; i < count[1] ; i ++ ){
12 nums[index++] = 1;
13 for( int i = 0 ; i < count[2] ; i ++ ){
14 nums[index++] = 2;
15 }
16 }
方法2
1 void sortColors1(vector<int>& nums){
2 int zero = -1;
3 int two = nums.size();
4 for( int i = 0 ; i < two ; ){
5 if( nums[i] == 1 )
6 i ++;
7 else if( nums[i] == 2 ){
8 two --;
9 swap( nums[i] , nums[two] );
10 }else{
11 assert( nums[i] == 0 );
12 zero ++;
13 swap( nums[zero] , nums[i] );
14 i ++;
15 }
16 }
17 }
相关
215 Kth Largest Element in an Array
[刷题] 75 Sort Colors的更多相关文章
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 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 (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- InfluxDB、Grafana等开源软件的监控后门
在使用手机APP的过程中,用户信息会不知不觉的被APP悄无声息的收集到云端,然后进行各种用户行为分析以及智能推荐,这是众所周知但秘而不宣的事. 在使用开源软件时,也存在悄悄收集用户使用信息,并且上报到 ...
- Python fire库使用
1.前要fire是python中用于生成命令行界面(Command Line Interfaces, CLIs)的工具 不需要做任何额外的工作,只需要从主模块中调用fire.Fire() 它会自动将你 ...
- HTML5和CSS3提高
一.HTML5的新特性 HTML5 的新增特性主要是针对于以前的不足,增加了一些新的标签.新的表单和新的表单属性等. 这些新特性都有兼容性问题,基本是 IE9+ 以上版本的浏览器才支持,如果不考虑兼容 ...
- C++并发与多线程学习笔记--单例设计模式、共享数据分析
设计模式 共享数据分析 call_once 设计模式 开发程序中的一些特殊写法,这些写法和常规写法不一样,但是程序灵活,维护起来方便,别人接管起来,阅读代码的时候都会很痛苦.用设计模式理念写出来的代码 ...
- 北航OO第一单元作业总结(1.1~1.3)
经过了三次作业之后,OO第一单元告一段落,作为一个蒟蒻,我初步了解了面向对象的编程思想,并将所学内容用于实践. 一.第一次作业 1.架构分析 本次作业需要完成的任务为简单多项式导函数的求解.表达式仅支 ...
- get_started_3dsctf_2016-Pwn
get_started_3dsctf_2016-Pwn 这个题确实有点坑,在本地能打,在远程就不能打了,于是我就换了另一种方法来做. 确这个题是没有动态链接库,且PIE是关的,所以程序的大部分地址已经 ...
- ret2dl64
ret2dl64 ret2dl64 与ret2dl32不同,ret2dl64需要知道libc. 检查保护: IDA看一看 read_got 被置为0,强制你使用ret2dlresolve. 我们先伪造 ...
- Ambassador-04- Mapping 资源
官方文档:https://www.getambassador.io/docs/latest/topics/using/intro-mappings/#resources Ambassador 通过Ma ...
- day9.函数2
一.函数对象 函数是第一类对象,第一等公民,函数对象即函数可以被当作变量去用. 具体分为四个方面: 1.可以被赋值 def func(): print('from func') f = func pr ...
- Spring-Gateway与Spring-Security在前后端分离项目中的实践
前言 网上貌似webflux这一套的SpringSecurity操作资料貌似很少. 自己研究了一波,记录下来做一点备忘,如果能帮到也在迷惑的人一点点,就更好了. 新项目是前后端分离的项目,前台vue, ...