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 ...
随机推荐
- 【IE6的疯狂之十】父级使用padding后子元素绝对定位的BUG
在前端开发中,经常会用到css的position:absolute来使层浮动,前通过left,top,right等属性来对层进行定位,但ie6对left,top,right等属性的解释和ie7,ie8 ...
- openstack私有云布署实践【16.3 Windows Server2008 R2 只有C盘分区镜像制作】
之所以要只有C盘分区镜像,是因为在创建VM或者调整云主机的硬盘大小时,它能自动扩容.无需人工介入 参考http://www.iyunv.com/thread-45149-1-1.html的灵感 ...
- CodeForces 669D Little Artem and Dance
模拟. 每个奇数走的步长都是一样的,每个偶数走的步长也是一样的. 记$num1$表示奇数走的步数,$num2$表示偶数走的步数.每次操作更新一下$num1$,$num2$.最后输出. #pragma ...
- Maven手动增加依赖jar到本地Maven仓库中
Apache Maven是一个项目管理及自动构建工具,有APache软件基金会提供.我们只要配置成功后就可以通过配置pom.xml添加所需依赖的jar包和类库,因为这些类库已经在我们配置的Maven仓 ...
- JWT 多网站单点登录,放弃session
多个网站之间的登录信息共享, 基于cookie - session的登录认证方式跨域等比较复杂.采用基于算法的认证方式, JWT(json web token)的方式. --------------- ...
- 批量修改安卓apk包名
1.准备工作 1.1 反编译工具apktool下载 1.2 java, android SDK安装 1.2 python安装 2.反编译现有包 apktool.bat d test.apk 3. 直接 ...
- maven GroupId 和ArtifactId的含义
GroupID是项目组织唯一的标识符,实际对应Java的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.
- 信息存储——当值X是2的非负整数n次幂时,如何表示成十六进制
十六进制表示法 当值X是2的非负整数n次幂时,很容易将X写成十六进制形式,只要记住X的二进制表示就是1后面跟n个0.十六进制数字0代表4个二进制0.所以当n表示成i+4j的形 ...
- Webstrom 连接svn报错怎么解决
Subversion: (Accessing URL: https://192.168.1.249:8443/svn/H5/seif ) Received fatal alert: handshak ...
- log4j.properties全配置 (转)
###############################log4j.properties############################### ##### Global Log Leve ...