【数组】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?
思路:
直观想法:遍历一遍数组,分别统计0,1,2三个元素的个数,然后根据各个元素的个数将原来的数组进行覆盖。这种方法需要对数组进行两轮遍历,第一轮遍历统计个数,第二轮遍历覆盖原数组。
改进想法:一次遍历即可将原数组排好序。一般来说排序算法都不太会是O(n)时间复杂度的,这道题之所以能做到这一点是因为只有3个元素。方法是:从前往后遍历数组,若遍历到0则将当前遍历到的0与从前往后最先的一个非0的数进行交换,若遍历到2则将当前遍历到的2与从后往前最先的一个非2的数进行交换,若遍历到1则继续往后遍历。对于0和2的情况,需要将当前元素与前面的或者后面的元素进行交换,所以需要用2个变量分别记住一前一后两个下标的位置,并不断进行更新。
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
var left=0,right=nums.length-1,zero=0,t; while(left<=right){
if(nums[left]==0){//nums[zero]=0
t=nums[left];
nums[left]=nums[zero];
nums[zero]=t;
zero++;
left++;
}else if(nums[left]==2){//nums[right]=2
t=nums[left];
nums[left]=nums[right];
nums[right]=t;
right--;
}else{
left++;
}
}
};
【数组】Sort Colors的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 【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: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- 【LeetCode】Sort Colors 数组排序
题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组.包括0,1,2 ...
- Sort colors系列
75. Sort Colors 问题描述: 给一个包含n个数字的数组,其中有0,1,2:排序使得所有相同的数字相邻,且按照0,1,2的顺序. 思路: (1)计数排序: 需要扫两遍数组,一遍统计个数,第 ...
- 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) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
随机推荐
- MySQL-5.7.10主主同步的安装和配置
目录 目录 1 1. 安装 1 2. 修改MySQL的root密码 4 3. mysqld_safe和mysql.server 4 4. 主主同步配置 4 4.1. 创建同步用户 4 4.2. my. ...
- HTML中submit和button的区别
submit是button的一个特例,也是button的一种,它把提交这个动作自动集成了. 如果表单在点击提交按钮后需要用JS进行处理(包括输入验证)后再提交的话,通常都必须把submit改成butt ...
- (转载)从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
本系列从Java程序员的角度,带大家理解前端Angular框架. 本文是入门篇.笔者认为亲自动手写代码做实验,是最有效最扎实的学习途径,而搭建开发环境是学习一门新技术最需要先学会的技能,是入门的前提. ...
- hdu 1014
我:题都看不懂 路人甲:这是随机数分配题目 路人乙:这是求生成元,求mod N的生成元,即生成元与N互质 路人丙:这是根据给出的递推公式算一下 0~ mod-1之间的数是否都有出现过,如果都出现了,那 ...
- springmvc elf8848
刚开始觉得孔浩讲得好,之后觉得开涛讲得好,现在觉得elf8848讲得好.其实只是自己学习的各个阶段 孔浩:环境搭建,做了个基础的CRUD 开涛:讲了Controller(不该看),注解,数据绑定,请求 ...
- Python学习-18.Python中的错误处理(三)
在某些情况下,我们需要定义自己的异常并且抛出 先定义一个错误: class MyError(BaseException): def __init__(self): pass 上面定义了一个叫MyErr ...
- Windows / Windows Phone 8.1 预留应用名称及应用上传
最近比较懒好久没有来这里跟大家聊了,WP 8.1 的 preview 发布已经有一阵子了,并且商店支持 8.1 应用也有一段时间了.我就把这篇 8.1 的应用商店预留提交作为 8.1 的一个开始吧. ...
- Nginx an upstream response is buffered to a temporary file
1.错误日志:warn:an upstream response is buffered to a temporary file 解决办法:增加fastcgi_buffers 8 4K; fa ...
- R语言和RStudio的一些用法,常用命令等
控制台: Up/down 回忆之前的命令 Ctrl+Up 回顾命令列表(可先输入前缀进行查找) 焦点: ctrl+ 移动焦点到source编辑器 ctrl+ 移动焦点到console ctrl+L 清 ...
- foreach写失效的问题
本文由作者张远道授权网易云社区发布. 坦白讲身为程序员,bug在所难免.有人讲,bug越多,说明程序员越伟大.这句话有它一定的道理. 因为从某方面讲,bug多了说明他的代码量也多. 言归正传,这里我记 ...