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 ...
随机推荐
- SpringBoot应用的属性管理
一.properties 配置文件 1.src/main/application.properties spring.profiles.active=dev spring.application.na ...
- 使用URL访问http服务器
一.概念定义 1.URI - 通用资源标识符 URI通常由三部分组成, 资源访问机制 存放资源的主机名 资源自身名称 如: http://www.baidu.com/html http://www.b ...
- ELK学习总结(2-2)单模式CRUD操作
------------------------------------------------------ 1.查看索引信息 请求命令: GET /library/_settings GET /li ...
- hadoop2.6.0实践:A03 例子验证
[hadoop@LexiaofeiN1 ~]$ hdfs dfs -ls /output/grep[hadoop@LexiaofeiN1 ~]$ hdfs dfs -rm -R /output/gre ...
- bad interpreter:No such file or directory 解决方法
今天在执行一个从网上考下来的脚本的时候,出现了下面的错误: Linux下面一个脚本死活也运行不了, 我检查了数遍,不可能有错. 提示:bad interpreter:No such file or d ...
- spring1——IOC之原理
spring框架的核心是IOC和AOP. 控制反转--IOC是一种编程思想,在spring中指的是对象的装配和管理交给了spring容器.这样做的好处是降低了系统之间的偶合度,既调用者不用再去创建被调 ...
- python/*args和**kwargs
*args和**kwargs #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值 ...
- SpringBoot(一):使用eclipse/idea创建springboot helloword工程
eclipse如何创建spring boot工程: 第一步:首先打开eclipse,找到图中的下图的中“下三角”符号,选中"working sets"(表示将会把eclipse中的 ...
- 如何用elementui去实现图片上传和表单提交,用axios的post方法
下面是在vue搭建的脚手架项目中的组件component文件夹下面的upload.vue文件中的内容 <!--这个组件主要用来研究upload这个elementui的上传插件组件--> & ...
- Vue项目模板--和--webpack自动化构建工具的---项目打包压缩使用
[首先安装node.js]: 1. 从node.js官网下载并安装node,安装过程很简单. 2. npm 版本需要大于 3.0,如果低于此版本需要升级它: # 查看版本 npm -v2.3.0 #升 ...