[leetcode]215. 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 sorted order, not the kth distinct element.
Example 1:
Input:[3,2,1,5,6,4]and k = 2
Output: 5
Example 2:
Input:[3,2,3,1,2,4,5,5,6]and k = 4
Output: 4
思路:
PriorityQueue
1. Have PriorityQueue created.
2. Insert all the elements into heap.
3. Call poll() k times.
代码:
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>(); // default order: ((o1, o2) -> o1 - o2)
for(int n : nums) {
heap.add(n);
if (heap.size() > k) {
heap.poll();
}
}
return heap.poll();
}
}
思路:
Quick Sort
核心思想是每次都要先找一个中枢点Pivot,然后遍历其他所有的数字,像这道题从小往大排的话,就把小于中枢点的数字放到左半边,把大于中枢点的放在右半边,这样中枢点是整个数组中第几大的数字就确定了,虽然左右两部分不一定是完全有序的,但是并不影响本题要求的结果,所以我们求出中枢点的位置,如果正好是k-1,那么直接返回该位置上的数字;如果大于k-1,说明要求的数字在左半部分,更新右边界,再求新的中枢点位置;反之则更新右半部分,求中枢点的位置
[leetcode]215. Kth Largest Element in an Array 数组中第k大的元素的更多相关文章
- [LeetCode] 215. 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] 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 ...
- 215 Kth Largest Element in an Array 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- LN : leetcode 215 Kth Largest Element in an Array
lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...
- 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 OJ 215. Kth Largest Element in an Array 堆排序求解
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an A ...
随机推荐
- 第12章 网络基础(1)_网络分层和TCP/IP协议族
1. 协议的概念 (1)计算机网络中实现通信必须有一些约定.如对速率.传输代码.代码结构.传输控制步骤和出错控制等约定,这些约定即被称为通信协议 (2)在两个节点之间要成功地进行通信,两个节点之间必须 ...
- asp.net webapi 自托管插件式服务(转)
asp.net webapi 自托管插件式服务 webapi问世已久,稀里糊涂的人哪它都当mvc来使,毕竟已mvc使用级别的经验就可以应对webapi. webapi和mvc在asp.net5时代 ...
- 网卡虚拟化技术:VMDq和SR-IOV
通常情况下,一个服务器上跑几十个虚机,对计算和网络的需求是很惊人的.前者促生了当下的多核技术发展,后者则不能简单的用多网卡来实现.试想,每个虚机如果都需要10G的交换能力,服务器要配置几十块物理网卡, ...
- QQ去除聊天框广告详解——2016.9 版
QQ聊天框广告很烦人,百度出来的一些方法早已过时,下面是博主整理出来的方法,供各位同学参考. 1.按键盘上的 Win+R 快捷键打开运行框,然后复制并粘贴 Application Data\Tence ...
- php上传文件涉及到的参数
php上传文件涉及到的参数: 几个参数调整: 0:文件上传时存放文件的临时目录.必须是 PHP 进程所有者用户可写的目录.如果未指定则 PHP 使用系统默认值 php.ini文件中uplo ...
- zabbix监控windows用户登陆情况
https://yq.aliyun.com/articles/511381 添加登录失败监控项: 特别注意:把类型设置为:文本格式,否则会报类型错误. eventlog[Security,," ...
- windows脚本测试
一. C:\Users\smc892h>systeminfo | findstr 物理内存物理内存总量: 12,167 MB可用的物理内存: 2,103 MB 二.截取字段 参考网站 ...
- HTML5 Canvas ( 文字的度量 ) measureText
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- xe7 c++builder 日期时间头文件函数大全 date
c++builde r时间日期函数大全,在头文件System.DateUtils.hpp,不过没有IncMonth,因为这个函数定义在System.SysUtils.hpp里头了,唉 date,dat ...
- VB 调用动态链接库
作为一种简单易用的Windows开发环境,Visual Basic从一推出就受到了广大编程人员的欢迎.它使 程序员不必再直接面对纷繁复杂的Windows消息,而可以将精力主要集中在程序功能的实现上,大 ...