[刷题] 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 ...
随机推荐
- Python 网络编程 C/S建立Socket连接
分为客户端和服务端 服务端 server.py 客户端 1 #coding=utf-8 2 import socket 3 4 client = socket.socket() #生成socket连接 ...
- 经典变长指令SIB
前言 ModR/M字段是用来进行内存寻址的,可当地址形如DS:[EAX + ECX*2 + 12345678]时,仅仅靠ModR/M字段,是描述不出来的. 这时就在ModR/M后面增加一个SIB字节, ...
- Mokito 单元测试与 Spring-Boot 集成测试
Mokito 单元测试与 Spring-Boot 集成测试 版本说明 Java:1.8 JUnit:5.x Mokito:3.x H2:1.4.200 spring-boot-starter-test ...
- Vim快速使用教程
Vim Vim是从vi发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富. Vi/Vim的使用 基本上分为三种模式,分别是命令模式(commad mode),输入模式(Inse ...
- RocketMq(一)初识
消息中间件基本上是互联网公司必用的一个中间件,为什么要使用MQ,当然是因为能给我们的系统带来很多好处. 消息队列简单来说是一种先进先出的数据结构,先简单认识下. 一.应用场景 消息中间件主要应用场景主 ...
- 本地+分布式Hadoop完整搭建过程
1 概述 Hadoop在大数据技术体系中极为重要,被誉为是改变世界的7个Java项目之一(剩下6个是Junit.Eclipse.Spring.Solr.HudsonAndJenkins.Android ...
- ACCESS常见错误场景
ACCESS常见错误场景 今天用access时发现好多报错的地方,emmm,比MySQL麻烦好多,有些甚至还要自己去配置环境 不吐槽了,进入正题: 报错场景一:您尝试执行不包含指定聚合函数的查询 第一 ...
- python 爬取王者荣耀英雄皮肤代码
import os, time, requests, json, re, sys from retrying import retry from urllib import parse "& ...
- Weekly Contest 139
1071. Greatest Common Divisor of Strings For strings S and T, we say "T divides S" if and ...
- DexClassLoader动态加载分析
转载自:http://www.blogfshare.com/dexclassloader.html 看到原来有把原始的dex文件加密保存,然后解密后使用DexClassLoader加载文件的方法,就来 ...