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 sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
题意:
给出一个未排序的数组,找出数组中第K大的元素。
方法:
使用优先级队列来解决。
优先级队列是不同于先进先出队列的另一种队列,每次从队列中取出的是具有最高优先权的元素。
如果不是提供comparator的话,优先队列中的元素默认按照自然顺序排列,
也就是数字小的默认在队列头,字符串按照字典顺序排列。
public class Solution {
//优先队列,
//遍历数组时将数字加入优先队列(堆),一旦堆的大小大于k就将堆顶元素去除,
//确保堆的大小为k,
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> p = new PriorityQueue<Integer>();
for(int i=0; i<nums.length;i++){
p.add(nums[i]);
if(p.size()>k) p.poll();
}
return p.poll();
}
}
leetcode 215. Kth Largest Element in an Array的更多相关文章
- 剑指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 数组中第k大的数字
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]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 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 ...
- C#版 - Leetcode 215. Kth Largest Element in an Array-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 【LeetCode】215. Kth Largest Element in an Array (2 solutions)
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
随机推荐
- java编程思想-java IO系统
一.输入和输出 编程语言的I/O类库中常使用流这个抽象概念,它代表任何有能力产出数据的数据源对象或者是有能力接收数据的接收端对象."流"屏蔽了实际的I/O设备中处理数据的细节. J ...
- scrapy3_ 安装指南
安装指南 安装Scrapy 注解 请先阅读 平台安装指南. 下列的安装步骤假定您已经安装好下列程序: Python 2.7 Python Package: pip and setuptools. 现在 ...
- centos linux安装telnet 过程及问题(源于内部tomcat网站,外部无法访问)
首先本地没有telnet客户端及服务器 root权限下安装 yum install telnet yum install telnet-server vi /etc/xinetd.d/telnet 这 ...
- 10月21上午PHP基础
新建的php文件必须要放在wamp安装目录下的www文件夹里.如果拿到别的地方,php无法运行,将显示错误. <?php?> //嵌入php的方式 <?php //嵌入php方式的开 ...
- 在项目中那个少用if else 语句,精简代码,便于维护的方法(1)
一般我在写一个函数的时候,可能需要一个回调函数,例如: function loadQtipCode(dom, title, content, width, showcb, hidecb) { $(do ...
- thinkphp安装 版本 3.1.3
基础版: 只有thinkphp基础运行功能 完整版:基础运行能力,还有图片.上传等各种处理类(建议下载完整版) 重要的三个变量 define('APP_DEBUG',True); // 定义应用目录d ...
- ecshop 如果缩略图为空,使用默认图片
引用:$row['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']); lib_common.php /** * 重新获 ...
- virtualbox中centos系统配置nat+host only上网
以前一直使用的是virtualbox的桥接模式,桥接模式的特点: 虚拟机和宿主机处于同等地位,就像是一台真实主机一样存在于局域网中,可以分配到一个网络中独立的IP. 虚拟机和宿主机之间能够互访. 如果 ...
- Multiple actions were found that match the request Web API
在WebAPI工程入口不对外公开的接口不能使用public. [HttpPost] public string PostRequest([FromBody] Model model) { /// } ...
- Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.
When I added the file in VS I forgot to set Build Action = None from the file properties.