原题链接在这里: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. 一致性 hash 算法

    consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...

  2. replaceCharactersInRange

    NSString 替换字符串中某一位置的文字  replaceCharactersInRange NSString 替换字符串中某一位置的文字 - (void)viewDidLoad { NSMuta ...

  3. 连接sql server的语句

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. android之listview

    首先建立res/layout/data_list.xml: 代码如下: <?xml version="1.0" encoding="utf-8"?> ...

  5. JavaScript - prototype 和 call 的理解

    prototype: 其实对象直接通过 [object.属性/方法]  来定义方法和属性也是可以的,但是会变成静态属性和静态方法,也就是调用的时候也是通过“.”来调用的.但如果通过prototype来 ...

  6. HTML5 javascript CSS3 jQuery Mobile一些好用的网站

    jQueryMobile:学习 http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html 百度 CDN: http://cdn.cod ...

  7. 前端编码风格规范(3)—— JavaScript 规范

    JavaScript 规范 全局命名空间污染与 IIFE 总是将代码包裹成一个 IIFE(Immediately-Invoked Function Expression),用以创建独立隔绝的定义域.这 ...

  8. PDO 学习与使用 ( 一 ) :PDO 对象、exec 方法、query 方法与防 SQL 注入

    1.安装 PDO 数据库抽象层 PDO - PHP Data Object 扩展类库为 PHP 访问数据库定义了一个轻量级的.一致性的接口,它提供了一个数据访问抽象层,针对不同的数据库服务器使用特定的 ...

  9. PHP文件操作 之读取一个文件(以二进制只读的方式打开)

    最近应用了文件的读取,顺便复习一下! //读取一个文件 $f = fopen($filename,'rb'); $f: 表示返回的一个资源句柄 $filename:要打开的文件路径 rb:参数,表示只 ...

  10. sqlserver log

    DBCC LOGINFODBCC log('QSSys', TYPE=2)goselect * from sys.fn_dblog(null,null)select [Dirty Pages],[Mi ...