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 ...
随机推荐
- Repeater控件中的三目运算
<asp:Repeater ID="rptimg" runat="server"> <ItemTemplate> ...
- Spring动态配置多数据源
Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...
- pdf转能编辑的word的方法
方法一:用汉王ocr文字识别软件,扫描文字,一页一页扫描,复制粘贴 方法二:将pdf文件拷贝到没有pdf阅读器的电脑上,同时你的office是2013,用word打开你的pdf文档,根据他的提示操作, ...
- Servlet编程-步步为营
[环境]eclipse j2ee;Tomcat 7.0; [模型1] package com.zhiqi; import ...; public class TestServlet extends H ...
- NBUT 1457 Sona(莫队算法+离散化)
[1457] Sona 时间限制: 5000 ms 内存限制: 65535 K 问题描述 Sona, Maven of the Strings. Of cause, she can play the ...
- netbeans使用技巧总结+快捷键大全
部分经常用的快捷键: 使用快捷键Alt+Enter显示修复错误的方法. 4.导航 a)Alt+Shift+O :转到类. b)Ctrl+Tab :在打开的源文件中进行切换. c)Alt+Right ...
- PHP 设计模式 笔记与总结(3)SPL 标准库
SPL 库的使用(PHP 标准库) 1. SplStack,SplQueue,SplHeap,SplFixedArray 等数据结构类 ① 栈(SplStack)(先进后出的数据结构) index.p ...
- Physically Based Render in Game 序
基于物理渲的渲染理论,从SIGGRAPH06被Naty Hoffman等人提出后,近年来也越来越多的被各大游戏公司所采用,几乎已经是次世代游戏的标准特性,也是每个3D游戏工作者所必备的知识,尽管每年S ...
- QQ音乐的各种相关API
QQ音乐的各种相关API 分类: oc2014-01-29 15:34 2676人阅读 评论(2) 收藏 举报 基本上论坛里做在线音乐的都在用百度的API,进来发现百度的API不仅歌曲的质量不可以保证 ...
- AP_总体业务及方案
AP关键业务点说明 关键业务点 说明 预付款余额收回 1. 在应付款管理系统中输入一张虚拟发票,该发票的目的是在系统中冲减对供应商的预付款额,其金额等于预付款的未核销金额,供应商为原供应商. 借:其他 ...