[LeetCode] 桶排序的特殊解,例 Sort Color
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.
class Solution {
public:
void sortColors(int A[], int n) {
}
};
像这种value被限定在一定范围内的排序,完全可以用桶排序做。
不过题目又加了限定,不让用桶排序。或者说,只允许单次遍历。
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。这种情况下我们可以定义三个指针:i, j, k,分别指向0段, 1段, 2段的末尾。当然,初识时i,j,k是重合的,赋值为-1。
k先走,不管遇到什么值,都将该值存为tmp,然后将当前位置赋值为2。因为就算tmp不是2,比如是0,k也要为这个0挪地方,所以把k位置置为2没有错。接着如果tmp <= 1,j前进并将j位赋值为1,道理一样,也是为了腾地方。最后处理i。
之所以必须k先走,其次j,最后i,第一是因为排序要求必须 0,1,2;第二,考虑这种情况:当前i, j , k重合,k前进一步遇到的是0。因为k先走,所以虽然k位被先置为2了,后面的j i 会将这个位置的值覆盖成为正确的值。
class Solution {
public:
void sortColors(int A[], int n) {
if(n == ) return;
int i = -, j = -, k = -, tmp = ;
while(k < n-){
tmp = A[++k];
A[k] = ;
if(tmp <= ) A[++j] = ;
if(tmp == ) A[++i] = ;
}
return;
}
};
小结
固定取值范围内的排序可以用桶排序,如果取值范围很小,只有几个值。我们可以直接定义等量的指针,通过这种方式一次遍历解决问题。
[LeetCode] 桶排序的特殊解,例 Sort Color的更多相关文章
- Bucket Sort - leetcode [桶排序]
桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- 线性时间的排序算法--桶排序(以leetcode164. Maximum Gap为例讲解)
前言 在比较排序的算法中,快速排序的性能最佳,时间复杂度是O(N*logN).因此,在使用比较排序时,时间复杂度的下限就是O(N*logN).而桶排序的时间复杂度是O(N+C),因为它的实现并不是基于 ...
- 计数排序与桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- 桶排序bucket sort
桶排序 (Bucket sort)或所谓的箱排序的原理是将数组分到有限数量的桶子里,然后对每个桶子再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序),最后将各个桶中的数据有序的 ...
- 451. Sort Characters By Frequency(桶排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- 【算法】桶排序(Bucket Sort)(九)
桶排序(Bucket Sort) 桶排序是计数排序的升级版.它利用了函数的映射关系,高效与否的关键就在于这个映射函数的确定.桶排序 (Bucket sort)的工作的原理:假设输入数据服从均匀分布,将 ...
- Linux下的sort排序命令详解(二)
有时候学习脚本,你会发现sort命令后面跟了一堆类似-k1,2,或者-k1.2 -k3.4的东东,有些匪夷所思.今天,我们就来搞定它—-k选项! 1 准备素材 [root@FDMdevBI opt]# ...
- leetcode算法题3:分组,让每个组的最小者,相加之后和最大。想知道桶排序是怎么样的吗?
/* Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
随机推荐
- Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
一些重要的知识: mybais-spring.jar及其提供的API: SqlSessionFactoryBean: SqlSessionFactory是由SqlSessionFactoryBuild ...
- servlet/jsp GET/POST
GET请求方式 当需要向服务器请求指定的资源时使用的方法 它不应该用于一些会造成副作用的操作中(在网络应用中用它来提交请求是一种常见的错误用法) 什么情况浏览器发送get请求 -在地址栏输入一个地址 ...
- crossplatform---Nodejs in Visual Studio Code 06.新建Module
1.开始 Node.js:https://nodejs.org 2.Moudle js编程中,由于大家可以直接在全局作用域中编写代码,使开发人员可以很容易的新建一个全局变量或这全局模块,这些全局变量或 ...
- 修改JSONArray里所有key的值
下面举一个代码的列子目的是实现如下功能: [{"userId":1,"userName":"plf"},{"userId" ...
- wicket基础应用(2)--wicket表单控件的使用
该文可以转载,但转载必须注明作者,出处: 作者:lhx1026 出处:http://lhx1026.iteye.com/ 这一章介绍wicket表单控件的简单应用 1.Label控件 这个应该说是最常 ...
- CreateJSのeasel.js(一)
CreateJS是基于HTML5开发的一套模块化的库和工具. 基于这些库,可以非常快捷地开发出基于HTML5的游戏.动画和交互应用. CreateJS为CreateJS库,可以说是一款为HTML5游戏 ...
- Maven之打包时配置文件替换
在JavaWeb项目中,使用maven打包.在打正式包时,需要手动修改数据库配置为线上环境的地址,这样每次修改起来比较麻烦. 搜索了一些资料后,大部分的做法或原理都是预先使用表达式占位符,然后在打包时 ...
- LPC43xx I2S
- [转]office2010一直卡在“正在受保护的视图中打开”
用Office 2010 打开文件遇到“”,如下图: 转自: 解决办法: 还可以使用下面的方法打开上图的受保护视图设置界面: 1. 启动 Word 2010 应用程序,单击[文件]按钮并选择[选项 ...
- 你可能不知道的python
1.如何循环获得下标,使用 enumerate ints = ['a','b','c','d','e','f'] for idx, val in enumerate(ints): print idx, ...