75. 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 supposed to use the library's sort function for this problem.
这题就是想把乱序的[2 2 1 0 1 1 0 2]
排列成有序的[0 0 1 1 1 2 2 2]
.
方法一:两趟循环,第一趟数0,1,2的个数,第二趟修改数组A, 思想简单且无编程难度.
\(O(2n)\) time, \(O(1)\) extra space.
// two-pass algorithm
// [ 2 2 1 0 1 1 0 2]
void sortColors(vector<int>& A) {
int c_0 = 0, c_1 = 0;
for (int i = 0; i < A.size(); i++)
if (A[i] == 0) c_0++;
else if (A[i] == 1) c_1++;
for (int i = 0; i < A.size(); i++)
if (i < c_0) A[i] = 0;
else if (i >= c_0 && i < (c_1 + c_0)) A[i] = 1;
else A[i] = 2;
}
方法二,人家的想法了,就是扫描数组,发现2就送入队尾方向,发现0就送队首方向,最后1自然就在中间,就排好了.这方法编程不容易.
设定 zero = 0, sec = A.size()-1 两个指针.
若 A[i] = 2, swap(A[i], A(sec--));
若 A[i] = 0, swap(A[i], A(zero++));
// one-pass algorithm
// [ 2 2 1 0 1 1 0 2]
void sortColors(vector<int>& A) {
int zero = 0, sec = A.size() - 1;
for (int i = 0; i <= sec; i++) {
while (A[i] == 2 && i < sec) swap(A[i], A[sec--]);
while (A[i] == 0 && i > zero) swap(A[i], A[zero++]);
}
}
75. Sort Colors(中等)的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 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
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 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 ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode OJ 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- GIT入门笔记(14)- 链接到远程仓库
1.远程仓库地址https://github.com/ 2.注册远程仓库账号 3.生成ssh-key,并配置到github 由于你的本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以, ...
- python 开发之路 - 入门
一. python 介绍 Python是著名的"龟叔"Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.1991年 发布Python ...
- python爬虫——分析天猫iphonX的销售数据
01.引言 这篇文章是我最近刚做的一个项目,会带领大家使用多种技术实现一个非常有趣的项目,该项目是关于苹果机(iphoneX)的销售数据分析,是网络爬虫和数据分析的综合应用项目.本项目会分别从天猫和京 ...
- tomcat增加处理线程数量
修改server.xml <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" ma ...
- Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...
- Centos7 安装python3
Centos7 安装python3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #安装sqlite-devel yum -y ...
- CMDB开发
浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...
- 1.2WEB API 跨域
详细请参考http://www.cnblogs.com/landeanfen/p/5177176.html 在项目上面使用Nuget安装 microsoft.aspnet.webapi.cors 在w ...
- beautiful soup
beautiful soup是一个可以从html或者xml文件中提取数据的python库,它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式. beautiful soup 会帮你节省数 ...
- HTTP首部扫盲
[TOC] 之前在做web开发时使用到HTTP首部的时候遇到不熟悉的都是现用现查,时间一长印象就不深刻了.最近在重读<图解HTTP>,其中有一章是专门讲解HTTP首部的,讲解的HTTP首部 ...