LintCode Kth Largest Element
原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/#
在LeetCode上也有一道,采用了标准的quickSelect 方法,另外写了一篇帖子,代码更加模块化。
采用的quickSelect方法,取出pivot, 比pivot 小的都放在左边,比pivot大的都放在右边,若是pivot左边包括pivot的个数恰巧等于k, 就返回pivot.
若是大于k, 就在左边递归寻找第k小的数,若是大于k,就在右边递归寻找 第 (k-left)小的数。
题目中说要找第k 大的数,其实就是找 numbers.size()-k+1小的数。
pivot取的是最后一个数,跳出loop时l所在位置一定比pivot大,需要换回来,调换l和end上的数。
为了保证跳出loop时l上的数比pivot大,中间的 while 循环条件是 numbers.get(l) < pivot 就移动l,另一个while loop 条件却是 number.get(r) >= pivot 移动r, 这是为了防止陷入infinite loop.
AC Java:
class Solution {
//param k : description of k
//param numbers : array of numbers
//return: description of return
public int kthLargestElement(int k, ArrayList<Integer> numbers) {
return findK(numbers.size()-k,numbers,0,numbers.size()-1);
}
private int findK(int k, ArrayList<Integer> numbers, int start, int end){
if(start >= end){
return numbers.get(start);
}
int m = partition(numbers, start, end);
if(m == k){
return numbers.get(m);
}else if(m < k){
return findK(k, numbers, m+1, end);
}else{
return findK(k, numbers, start, m-1);
}
}
private int partition(ArrayList<Integer> numbers, int start, int end){
int pivot = numbers.get(start);
int m = start;
int n = start + 1;
while(n<=end){
if(numbers.get(n) < pivot){
swap(numbers, ++m, n);
}
n++;
}
swap(numbers, start, m);
return m;
}
private void swap(ArrayList<Integer> numbers, int l, int r){
int temp = numbers.get(l);
numbers.set(l,numbers.get(r));
numbers.set(r,temp);
}
};
另外一种写法:
class Solution {
//param k : description of k
//param numbers : array of numbers
//return: description of return
public int kthLargestElement(int k, ArrayList<Integer> numbers) {
if(numbers == null || numbers.size() == 0 || k<1){
return 0;
}
return getKth(numbers.size()-k+1, numbers, 0, numbers.size()-1);
}
private int getKth(int k, ArrayList<Integer> numbers, int start, int end){
int pivot = numbers.get(end);
int l = start;
int r = end;
while(true){
while(numbers.get(l) < pivot && l<r){
l++;
}
while(numbers.get(r) >= pivot && r>l){
r--;
}
if(l == r){
break;
}
swap(numbers, l, r);
}
//l element is larger than pivot, swap it with pivot
swap(numbers, l, end);
if(k == l+1){
return numbers.get(l);
}else if(k < l+1){
return getKth(k, numbers, start, l-1);
}else{
return getKth(k, numbers, l+1, end);
}
}
private void swap(ArrayList<Integer> numbers, int l, int r){
int temp = numbers.get(l);
numbers.set(l,numbers.get(r));
numbers.set(r,temp);
}
};
LintCode Kth Largest Element的更多相关文章
- Lintcode: Kth Largest Element 解题报告
Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- leetcode 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Java for LeetCode 215 Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 【leetcode】Kth Largest Element in an Array (middle)☆
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- Kth Largest Element in an Array - LeetCode
examination questions Find the kth largest element in an unsorted array. Note that it is the kth lar ...
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
- 215. Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
随机推荐
- CentOS 下安装配置mongodb
先从这个地方下载 http://www.mongodb.org/downloads 然后#tar -zxvf mongodb-linux-x86_64-2.4.8.tgz # sudo mv mo ...
- java图片处理——多张图片合成一张Gif图片并播放或Gif拆分成多张图片
1.多张jpg图合成gif动画 /** * 把多张jpg图片合成一张 * @param pic String[] 多个jpg文件名 包含路径 * @param newPic String 生成的gif ...
- SVN的学习和安装
SVN分为服务器版本和客户端版本 服务器:VISUALSVN SERVER https://www.visualsvn.com/server/download/ 安装和配置(都很简单,只要不断的下一步 ...
- 配置SMarty解析
在 common/main.php中配置 View 组件 'view' => [ 'renderers' => [ 'tpl' => [ 'class' => 'yii\sma ...
- 《Pro Git》笔记2:Git基础操作
第二章 Git基础 Git基础包括:版本库的创建和获取,文件添加修改提交等基本操作,状态查询,远程版本库管理和同步,打标签. 1.取得项目的Git版本库 基于Git的工作流要以Git版本库为基础,即可 ...
- [故障处理]联想笔记本故障0x0000007B
同事笔记本故障,莫名其妙的快捷方式就找不到了.开始程序中的内容也无法正常查看. 解决步骤: 1.怀疑用户配置的问题,新建一个用户,没有解决. 2.使用自带的一键恢复ThinkVantage,恢复后,重 ...
- AMD PerfStudio
用PerfStudio 抓DX11 OIT
- jQuery Ajax 确定 form 表单 submit 提交成功
使用 jQuery 提交表单,可以使用 同步方式(async: false). a.html 是 html 文件,a.php 是服务端文件,把 a.html 中表单的数据提交到 a.php 中,在提交 ...
- sql group by+字段
MySQL GROUP BY 语句 GROUP BY 语句根据一个或多个列对结果集进行分组. 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数. 2.在group by的分组字段上,我 ...
- NSDate 获取明天、后天的日期
NSDate * senddate=[NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIde ...