LeetCode OJ 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 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分别代表三种颜色red,white,blue。对这三种颜色排序,使得red在最前面,blue在最后面。最好的办法是一次遍历,使用固定的空间。
【思路】
1. 排序法
这是效率最低的方法,我们要像排序其他数组一样对有三种值(0,1,2)的数组增序排序,时间复杂度在O(n2);
2. 计数法
先遍历一遍数组,统计每种颜色的个数m,n,l,然后进行第二次遍历,写入m个0,n个1,l个2;时间复杂度为O(n);
3. 标记法
我们设置两个标记,分别记录0的结束位置和2的开始位置,遍历当前数组,如果为1则继续向前,如果为0则把它放到0的结束位置之后,如果为2就把它放到2的开始位置之前。这样经过一遍遍历就可以把不同的颜色分开。举个例子如下:
【java代码】
public class Solution {
public void sortColors(int[] nums) {
if(nums == null || nums.length == 0) return; int lastred = -1;
int firstblue = nums.length;
int i = 0; while(i < firstblue){
if(nums[i] == 0){
if(i == lastred + 1)
lastred = i++;
else if(i > lastred + 1){
nums[++lastred] = 0;
nums[i++] = 1;
}
}
else if(nums[i] == 2){
if(nums[firstblue-1] != 2){
nums[i] = nums[firstblue-1];
nums[--firstblue] = 2;
}
else firstblue--;
}
else i++;
}
}
}
LeetCode OJ 75. 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- LeetCode OJ:Sort Colors(排序颜色)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode OJ平台Sort Colors讨论主题算法
原题如下面,这意味着无序排列(由0,1,2组成).一号通.组织成若干阵列0-几个1-几个2这样的序列. Given an array with n objects colored red, white ...
- 【leetcode】75. Sort Colors
题目如下: 解题思路:我的解题思路是遍历数组,遇到0删除该元素并插入到数组头部,遇到1则不处理,遇到2删除该元素并插入到数组尾部. 代码如下: class Solution(object): def ...
- 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 ...
随机推荐
- MyEclipse 中文注释乱码
window-->preference-->general-->content type然后在<Content Types>中展开每一个子项,并在<Default ...
- sql时间转换函数--备忘
总是忘记 一.语法: CAST (expression AS data_type) 参数说明: expression:任何有效的SQServer表达式. AS:用于分隔两个参数,在AS之前的是要处理的 ...
- Hibernate——脏检查和缓存清理机制
Session到底是如何进行脏检查的呢? 当一个Customer对象被加入到Session缓存中时,Session会为Customer对象的值类型的属性复制一份快照.当Session清理缓存时,会先进 ...
- vb.net 结束进程
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ...
- ubuntu 14.04中文分词 结巴分词
在自然语言处理中,很常见的是要对文本数据进行分词处理.博主是代码小白,目前只是用python作为数据预处理的工具,而按照结巴中文分词的导语:做最好的python中文分词组件“jieba”.因而博主也就 ...
- CSS 待解决问题
记录一些待解决的问题 1. 关于 table 边框参差不齐问题 乍一看表头的边框有 间隙, 下边框也有点没有对其的问题. 解决方案: 给 table 添加: border-collapse: col ...
- Spring Security(19)——对Acl的支持
目录 1.1 准备工作 1.2 表功能介绍 1.2.1 表acl_sid 1.2.2 表acl_class 1.2.3 表acl_obj ...
- Abstract和Virtual
在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在来讨论一下二者的区别. 它们有一个共同点:二者都是用来修饰父类的,只能作为基类使用,通过覆盖父类的定义,让子类与overri ...
- 学习任务在继续...css...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 洛谷-乘积最大-NOIP2000提高组复赛
题目描述 Description 今年是国际数学联盟确定的“2000――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰90周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学智力竞赛的活动,你 ...