原题链接在这里: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的更多相关文章

  1. Lintcode: Kth Largest Element 解题报告

    Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. 【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 ...

  6. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  7. 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 ...

  8. 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 ...

  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 ...

随机推荐

  1. MyEclipse 10 之下Web Service 的创建和实现

    (一)Web service服务端开发 1. 新建一个Web service project, 菜单New -> Web Service Project, 2. 新建一个 Java Bean, ...

  2. 2015寒假ACM训练计划

    1月26号至3月4号 每天给自己一个计划.做有意义的事情,不要浪费时间. 8:00——11:30 acm训练 11:30——13:00 午休 13:00——17:30  acm训练 17:30——18 ...

  3. [转]C# WinForm动态调用远程Web服务

    本文转自:http://blog.csdn.net/muyangjun/article/details/7930871 1.添加服务引用 2.在弹出的添加服务引用对话框地址栏中输入WebService ...

  4. IOS第六天(3:scrollView 图片轮播器)

    IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...

  5. Editplus从下载到使用

    ☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...

  6. iOS textFiled 在storyBoard中的使用

    step 1. 在UITableViewCotroller的xib中设置一个静态表格,在Utilities里选择属性检查器(第四个啦)设置属性,content : static cells. styl ...

  7. snowflake

    snowflake在分布式系统中生成全局id

  8. Codeforces Round #357 (Div. 2) E 计算几何

    传说中做cf不补题等于没做 于是第一次补...这次的cf没有做出来DE D题的描述神奇 到现在也没有看懂 于是只补了E 每次div2都是hack前2~3题 终于打出一次hack后的三题了...希望以后 ...

  9. 《Java核心技术卷一》笔记 多线程同步(底层实现)

    一.锁的基本原理 多个线程同时对共享的同一数据存取 ,在这种竞争条件下如果不进行同步很可能会造成数据的讹误. 例如:有一个共享变量int sum=0, 一个线程正调用 sum+=10,另一个线程正好也 ...

  10. PHP+jQuery 注册模块开发

    /* ******* 环境: Apache2.2.8 + PHP5.2.6 + MySQL5.0.51b + jQuery-1.8.3.min.js ************** 其他组件:Zend_ ...