[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 ...
随机推荐
- Qt 5.3.1 版本应用程序的发布问题
问题描述:用过Qt的朋友,都知道,完成的Qt程序,只能在QT环境里运行.在debug环境里,没有配置环境路线的情况下,必须包含多个dll库,然而每个dll库的大小确实很大的.但有时候还是会失败的,在一 ...
- paip. http 405 的解决..
paip. http 405 的解决.. get>>> POST 或者 syeofe.. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:a ...
- Maven学习总结(二)——Maven项目构建过程练习_转载
上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立M ...
- bzoj 3517: 翻硬币
3517: 翻硬币 Time Limit: 1 Sec Memory Limit: 128 MB Description 有一个n行n列的棋盘,每个格子上都有一个硬币,且n为偶数.每个硬币要么是正面 ...
- Android视觉规范-间距规范与文字规范单位换算(dip、sp与px)
http://blog.csdn.net/shimiso/article/details/29826073 1.dip与px Android工程师在写页面时,margin值的单位是dip,而视觉设计师 ...
- 利用同一 ASP.NET 的多个代码框架
2012 年,Microsoft 推出了两个添加到 ASP.NET 工具包的新框架:Web API 和 SignalR. 这两个框架为开发环境带来独特的开发方式,每个框架都有自身的独特之处: Web ...
- asp.net MVC的EF与easyui DataGrid数据绑定
页面代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...
- Android——程序移植 相关知识总结贴
android 移植笔记有感 http://www.apkbus.com/android-11842-1-1.html Android振动器系统结构和移植和调试 http://www.apkbus ...
- Eclipse 基于接口编程的时候,快速跳转到实现类的方法(图文)
Eclipse 基于接口编程的时候,要跳转到实现类很麻烦,其实Eclipse已经实现该功能. 只要按照Ctrl键,把鼠标的光标放在要跳转的方法上面,第一个是跳转到接口里面,第二个方法是跳转到实现类的位 ...
- C#人爱学不学9[C#5.0异步实例+WPF自己的MVVM Async应用 1/12]
文章摘要: 1. 通过简单DEMO.让读者理解Task和Task<T> 学习过程中,掌握async和await 2. 理解同步和异步的执行 3. Task.Factory.Start ...