[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 ...
随机推荐
- SpringMVC从一个controller跳转到另一个controller
return "redirect:……路径……"; @RequestMapping(value = "/index", method = RequestMeth ...
- 通过Greasemonkey实现网页图片自动点击
昨天受一个朋友所托,实现了一个在特定网页自动点击某超链接图片实现网页跳转功能的JavaScript脚本. 工具就是Firefox的Greasemonkey扩展插件.代码如下: // ==UserScr ...
- 1121 if条件语句练习--输入年月日判断执行
<script type="text/javascript"> var a=prompt("请输入一个年","请输入年份"); ...
- celery简单应用
写作背景介绍 在celery简单入门中已经介绍了写作的背景,这篇文章主要是深入介绍celery的使用技巧.在实际的项目中我们需要明确前后台的分界线,因此我们的celery编写的时候就应该是分成前后台两 ...
- paip.python3 的类使用跟python2 的不同之处
paip.python3 的类使用跟python2 的不同之处 #------python3的写法而且使用.. #class syllable(BaseClassA, BaseClassB): cla ...
- 公钥,私钥,SSL(讲的很生动) (转) 对称加密、非对称加密初探
最近开始做消息推送,有不少概念性的东西需要知道,首先应该了解的是密钥.这片文章很清晰的讲解了对称密钥.非对称密钥.ssl的知识. 原文地址:http://chenling1018.blog.163.c ...
- 【干货】个人工作文档节选:XAML MVVM 框架易用性细节优化Tips
1 易用性细节优化 1.1 代码片段 在ViewModel内,会有大量重复性的在Property set中激发 INotifyPropertyChanged.PropertyChanged 事件 ...
- Linux内核源码详解——命令篇之iostat[zz]
本文主要分析了Linux的iostat命令的源码,iostat的主要功能见博客:性能测试进阶指南——基础篇之磁盘IO iostat源码共563行,应该算是Linux系统命令代码比较少的了.源代码中主要 ...
- 【转】开放api接口签名验证
不要急,源代码分享在最底部,先问大家一个问题,你在写开放的API接口时是如何保证数据的安全性的?先来看看有哪些安全性问题在开放的api接口中,我们通过http Post或者Get方式请求服务器的时候, ...
- WPF 模板
一.DataTemplate(数据模板)1.引用命名空间xmlns:别名="clr-namespace:命名空间" 2.调用命名空间下的类别和属性<Window.Resour ...