基于快速排序:

 class Solution {
public:
/*
* param k : description of k
* param nums : description of array and index 0 ~ n-1
* return: description of return
*/
int kthLargestElement(int k, vector<int> nums) {
// write your code here
int left = , right = nums.size() - ;
while (true) {
int pos = partition(nums, left, right);
if (pos == k - ) return nums[pos];
if (pos < k - ) left = pos + ;
if (pos > k - ) right = pos - ;
}
}
private:
int partition(vector<int>& nums, int left, int right) {
int pivot = nums[left];
int l = left + , r = right;
while (l <= r) {
if (nums[l] < pivot && nums[r] > pivot)
swap(nums[l++], nums[r--]);
if (nums[l] >= pivot) l++;
if (nums[r] <= pivot) r--;
}
swap(nums[left], nums[r]);
return r;
}
};

基于最大堆:

 class Solution {
public:
/*
* param k : description of k
* param nums : description of array and index 0 ~ n-1
* return: description of return
*/
inline int left(int idx) {
return (idx << ) + ;
}
inline int right(int idx) {
return (idx << ) + ;
}
void max_heapify(vector<int>& nums, int idx) {
int largest = idx;
int l = left(idx), r = right(idx);
if (l < heap_size && nums[l] > nums[largest])
largest = l;
if (r < heap_size && nums[r] > nums[largest])
largest = r;
if (largest != idx) {
swap(nums[idx], nums[largest]);
max_heapify(nums, largest);
}
}
void build_max_heap(vector<int>& nums) {
heap_size = nums.size();
for (int i = (heap_size >> ) - ; i >= ; i--)
max_heapify(nums, i);
}
int kthLargestElement(int k, vector<int> nums) {
// write your code here
build_max_heap(nums);
for (int i = ; i < k; i++) {
swap(nums[], nums[heap_size - ]);
heap_size--;
max_heapify(nums, );
}
return nums[heap_size];
}
private:
int heap_size;
};

[LintCode] 第k大元素的更多相关文章

  1. LintCode——第K大元素

    第K大元素:在数组num中找到第k大的元素(可以交换数组中的元素的位置) 样例: 数组 [9,3,2,4,8],第三大的元素是 4 数组 [1,2,3,4,5],第一大的元素是 5,第二大的元素是 4 ...

  2. lintcode 中等题:kth-largest-element 第k大元素

    题目 第k大元素 在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注 ...

  3. 【转载】两个排序数组的中位数 / 第K大元素(Median of Two Sorted Arrays)

    转自 http://blog.csdn.net/zxzxy1988/article/details/8587244 给定两个已经排序好的数组(可能为空),找到两者所有元素中第k大的元素.另外一种更加具 ...

  4. 寻找两个已序数组中的第k大元素

    寻找两个已序数组中的第k大元素 1.问题描述 给定两个数组与,其大小分别为.,假定它们都是已按照增序排序的数组,我们用尽可能快的方法去求两个数组合并后第大的元素,其中,.例如,对于数组,.我们记第大的 ...

  5. 面试题:求第K大元素(topK)?

    一.引言二.普通算法算法A:算法B:三.较好算法算法C:算法D:四.总结 一.引言 ​ 这就是类似求Top(K)问题,什么意思呢?怎么在无序数组中找到第几(K)大元素?我们这里不考虑海量数据,能装入内 ...

  6. java优先级队列的使用 leecode.703.数据流中的第K大元素

    //设计一个找到数据流中第K大元素的类(class). //注意是排序后的第K大元素,不是第K个不同的元素. class KthLargest { private PriorityQueue<I ...

  7. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  8. 数据流中的第k大元素的golang实现

    设计一个找到数据流中第K大元素的类(class).注意是排序后的第K大元素,不是第K个不同的元素. 你的 KthLargest 类需要一个同时接收整数 k 和整数数组nums 的构造器,它包含数据流中 ...

  9. 第k大元素

    在数组中找到第k大的元素 样例 给出数组[9,3,2,4,8],第三大的元素是4 给出数组 [1,2,3,4,5],第一大的元素是5,第二大的元素是4,第三大的元素是3,以此类推 注意 你可以交换数组 ...

随机推荐

  1. java基础讲解08-----类和对象

    1.什么是面向对象? 面向对象设计的实质 就是对现实世界的对象进行建模操作. 现实的生活中,随处可见的一种事物就是对象,对象是事物存在的实体,通常我们将会对对象划分为两个部分,静态部分和动态部分.比如 ...

  2. centos 7 生成文件名乱码的问题如何解决?

    在应用脚本生成文件时,发现生成的文件名称出现乱码(似麻将一样).刚开始找来找去,以为是复制粘贴的原因,复制时复制了特殊字符导致的,结果修改源文件后发现生成的文件名还是乱码.后来在执行脚本时,提示&qu ...

  3. 各种MQTT server功能比較

    this page attempts to document the features that various MQTT servers (brokers) support. This is spe ...

  4. docker容器互连

    三种方式 1.使用容器连接的示例如下: $ docker run --name some-app --link itbilu-mysql:mysql -d application-that-uses- ...

  5. 总结文件操作函数(一)-C语言

    在进程一開始执行,就自己主动打开了三个相应设备的文件.它们是标准输入.输出.错误流.分别用全局文件指针stdin.stdout.stderr表示,相应的文件描写叙述符为0.1.2:stdin具有可读属 ...

  6. python 2,3版本自动识别导入

     import sys if str(sys.version[0]) == "3":    from urllib.parse import quote_plus    from  ...

  7. SET IDENTITY_INSERT <Table Name> ON/OFF 转载

    This command is from SQL Server.  This command is to enable the users to set their own value for IDE ...

  8. java中for循环执行过程

    for (int j = 0; j < newSize; j++) { //执行todo } 1.首先变量j初始化为0 2.然后j=0的值跟newSize进行比较,假如为true,则执行{}里面 ...

  9. curl 远程下载图片

    <?php echo httpcopy("http://www.baidu.com/img/baidu_sylogo1.gif"); function httpcopy($u ...

  10. 使用SWT技术的跨平台移动应用开发库Tabris

    1. http://developer.eclipsesource.com/tabris/ 2. 多平台.原生程序级别的性能 3. 开源/教育性的项目免费,意思就是商业项目收费 4. 目标,让懂jav ...