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 ...
随机推荐
- 百度移动搜索自动转码太坑爹,JS跳转地址会被抓取
这段时间碰到个很崩溃的问题,一个页面通过 script 加载请求服务端进行统计再输出js进行跳转,分为两个步骤分别统计, 打开页面通过script 请求远程服务器进行统计并输出要通过js使页面跳转的最 ...
- 【锋利的Jquery】读书笔记三
DOM操作 三个方面;DOM core html-dom css-dom 注意点: 删除事件中 三种删除节点的方法 remove detach empty remove不解释 de ...
- [SOJ] 畅通工程续
Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多 ...
- sendUserActionEvent() mView== null after clicking on button
this is not a problem related to your code, but related to S4 android version. Same question has bee ...
- oracle查询排序后的前几条记录
select * from (select * from table order by 字段名 desc) where rownum<你要查的记录条数,这样才能符合条件.
- MySQL字段联合去重sql
MySQL字段联合去重sql 例如数据 id,name1,name2 1 a x 2 a y 3 b x 4 a y 5 a x 联合去重name1,name2的结果为 id,name1,name2 ...
- python setup.py install 报错ImportError: No module named setuptools
学习光荣之路python课程时,使用python setup.py install安装其他模块时,第一次安装某模块成功了.安装另一模块却报错ImportError: No module named s ...
- sql server 查询表基本信息sql
SELECT c.name,t.name TYPE,c.max_length,c.precision,c.scale,p.value FROM sys.systypes t INNER JOIN sy ...
- Mac下MySQL的安装与配置
之前一直用的是阿里云的服务器,在服务器上装了一个MySQL,但是今天发现到期了,而且续费时发现之前的大学生优惠不能用了,可是明明到6月份,大学生才毕业啊,shit!!!所以没办法只能在自己电脑上装一个 ...
- Learning from the CakePHP source code - Part I
最近开始痛定思痛,研究cakephp的源码. 成长的路上从来没有捷径,没有小聪明. 只有傻傻的努力,你才能听到到成长的声音. 下面这篇文章虽然过时了,但是还是可以看到作者的精神,仿佛与作者隔着时空的交 ...