[刷题] 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 ...
随机推荐
- 普通 Javaweb项目模板的搭建
普通 Javaweb项目模板的搭建 1. 创建一个web项目模板的maven项目 2.配置 Tomcat 服务器 3.先测试一下该空项目 4.注入 maven 依赖 5.创建项目的包结构 6.编写基础 ...
- 系统编程-信号-信号发送kill、raise、alarm
信号发送 kill 和 raise函数 kill函数参数详解: 实验1 raise和kill 的使用 #include <stdio.h> #include <signal.h> ...
- 一文彻底掌握Apache Hudi的主键和分区配置
1. 介绍 Hudi中的每个记录都由HoodieKey唯一标识,HoodieKey由记录键和记录所属的分区路径组成.基于此设计Hudi可以将更新和删除快速应用于指定记录.Hudi使用分区路径字段对数据 ...
- 解决Linux无法读写U盘中的NTFS问题
1 问题描述 由于笔者因为某些需要把Windows装在了U盘上面(在这里建议一下如果有需要请使用固态U盘),在Linux下挂载时,能读取但并不能写. 2 尝试的解决方案 2.1 remount 一开始 ...
- 关于Redis客户端显示中文或中文乱码问题的解决
关于Redis客户端不显示中文或者显示中文乱码,网上有些解决办法不全面的,都是漏了步骤不讲,简直瞎扯.在此记录一下解决方式. 刚开始使用Redis客户端的时候,本身就是默认不支持显示中文的. 例如,进 ...
- 三个dom xss常用tips
分享dom xss的三个案例 (1)javascript里面过滤单引号和双引号? 搭建环境: 只是过滤了单引号和双引号是可以xss的: 使用<>闭合script即可 </script ...
- 1037 Magic Coupon
The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...
- D - The Frog's Games (二分)
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...
- PHP--date转成时间戳,time()获取的是秒数
date("Y-m-d H:i:s"); //如果存成datetime型在MYSQL中 必须用这种格式 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳.strtotime ...
- 【CompletableFuture】CompletableFuture中join()和get()方法的区别
一.相同点: join()和get()方法都是用来获取CompletableFuture异步之后的返回值 二.区别: 1.join()方法抛出的是uncheck异常(即未经检查的异常),不会强制开发者 ...