【LeetCode】075. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:
You are not suppose to use the library's sort function for this problem.
Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
Could you come up with an one-pass algorithm using only constant space?
解1 计数排序
class Solution {
public:
void sortColors(vector<int>& v) {
vector<int> count(, );
for (int i = ; i < v.size(); ++i) {
if (v[i] == ) {
++count[];
} else if (v[i] == ) {
++count[];
} else {
++count[];
}
}
int it = ;
for (int i = ; i < ; ++i) {
for (int j = ; j < count[i]; ++j) {
v[it++] = i;
}
}
}
};
解2 双指针思想的延伸
class Solution {
public:
void sortColors(vector<int>& v) {
int low = , mid = , high = v.size() - ;
while (mid <= high) {
if (v[mid] == ) {
swap(v[mid], v[low]);
++low;
++mid;
} else if (v[mid] == ) {
swap(v[mid], v[high]);
--high;
} else {
++mid;
}
}
}
};
解3 比较有技巧
class Solution {
public:
void sortColors(vector<int>& v) {
int n0 = -, n1 = -, n2 = -;
for (int i = ; i < v.size(); ++i) {
if (v[i] == ) {
v[++n2] = ;
v[++n1] = ;
v[++n0] = ;
} else if (v[i] == ) {
v[++n2] = ;
v[++n1] = ;
} else {
v[++n2] = ;
}
}
}
};
【LeetCode】075. Sort Colors的更多相关文章
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- 【leetcode】75. Sort Colors
题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...
- 【leetcode】905. Sort Array By Parity
题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- 【LeetCode】922. Sort Array By Parity II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...
- 【LeetCode】451. Sort Characters By Frequency 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 优先级队列 排序 日期 题目地址:https: ...
随机推荐
- ES集群性能调优链接汇总
1. 集群稳定性的一些问题(一定量数据后集群变得迟钝) https://elasticsearch.cn/question/84 2. ELK 性能(2) — 如何在大业务量下保持 Elasticse ...
- 【LeetCode】【动态规划】表格移动问题
前言 这里总结了两道表格移动的问题,分别是:Unique Paths 和 题一:Unique Paths 描述 A robot is located at the top-left corner of ...
- java的服务端与客户端通信(1)
一.理解socket 1.1什么是socket? socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄.应用程序通常通过"套接字"向网络 ...
- Linux基本命令 网络命令
概述 网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nslookup, traceroute, finger, telnet, ...
- springboot-整合freemarker
freemarker是一个页面模板引擎.用springboot整合freemarker的方式如以下步骤: 1.在创建springboot的项目的时候,选择freemarker的组件,或者自己手动在ma ...
- HISAT2的运用
功能: 用于有参考基因组存在的比对工具(适用于whole-genome, transcriptome, and exome sequencing data) 用法: hisat2-build [opt ...
- Prometheus Monitoring System & Time Series Database
什么是 TSDB (Time Series Database): 我们可以简单的理解为.一个优化后用来处理时间序列数据的软件,并且数据中的数组是由时间进行索引的. 时间序列数据库的特点: 大部分时间都 ...
- 【codevs1069】关押罪犯[noip2010](并查集)
题目描述 Description S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极 不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨 ...
- Java -- 数据库 多表操作,1对多,多对多,1对1。 基于dbutils框架
1. 1对多,部门--员工 为例, 多的一方建外键. domain,建立bean对象 public class Department { private String id; private Stri ...
- Mysql -- SQL常用命令实例
sql: structured query language(结构化查询语言) 用户名和密码:root 创建一个名称为mydb1的数据库. create database mydb1; 查看所有数据库 ...