【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.
题解1:玩赖的写法,直接调用java中的sort函数,之后选取倒数第k个元素,即为所有。
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
int len = nums.length;
return nums[len - k];
}
题解2:使用优先队列来实现一个小根堆。堆未满时,直接插入nums[i];堆满了,执行poll(),先删除队列中的一个元素,在插入新元素。
在java中,常见的队列操作以及它们的区别如下所示:
插入 | offer | 向队列插入元素,在一个满的队列中加入一个新项会返回false。 |
add | 向队列插入元素,在一个满的队列中加入一个新项会抛出异常。 | |
删除 | poll | 从队列中删除第一个元素,在队列为空时返回null。 |
remove | 从队列中删除第一个元素,在队列为空时会抛出异常。 | |
查看队头元素 | peek | 在队列的头部查询元素,在队列为空时返回null。 |
element | 在队列的头部查询元素。在队列为空时会抛出异常。 |
public int findKthLargest2(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>(nums.length,(w1, w2)->w1.compareTo(w2));
for(int j=0;j<nums.length;j++){
minHeap.add(nums[j]);
if(minHeap.size()>k){
minHeap.poll();
}
}
return minHeap.peek();
}
完整代码如下:
package medium; import java.util.Arrays;
import java.util.PriorityQueue; public class L215KthLargestElementinanArray { public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
int len = nums.length;
return nums[len - k];
} public int findKthLargest2(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>(nums.length, (w1, w2) -> w1.compareTo(w2));
for (int j = 0; j < nums.length; j++) {
minHeap.add(nums[j]);
if (minHeap.size() > k) {
minHeap.poll();
}
}
return minHeap.peek();
} public static void main(String[] args) {
L215KthLargestElementinanArray l215 = new L215KthLargestElementinanArray();
int[] nums = { 2, 1, 3, 4, 5 };
int k = 1;
int number = l215.findKthLargest(nums, k);
System.out.println(number);
System.out.println("!!!!!!!!!!!!");
int num2 = l215.findKthLargest2(nums, k);
System.out.println(num2);
}
}
【leetcode】215. Kth Largest Element in an Array的更多相关文章
- 【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 ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- 【刷题-LeetCode】215. Kth Largest Element in an Array
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
- 【easy】215. Kth Largest Element in an Array 第K大的数
class Solution { public: int quicksort(vector<int>& nums, int start, int end, int k){ int ...
- 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 ...
- 【LeetCode】703. Kth Largest Element in a Stream 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- LeetCode OJ 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 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
随机推荐
- spark 例子倒排索引
spark 例子倒排索引 例子描述: [倒排索引(InvertedIndex)] 这个例子是在一本讲spark书中看到的,但是样例代码写的太java化,没有函数式编程风格,于是问了些高手,教我写了份函 ...
- 20155232 2016-2017-2 《Java程序设计》第2周学习总结
20155232 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 类型 基本类型 整数(short.int.long) 字节(byte) -128~127 字 ...
- #2017-2018-1 20155327 《信息安全系统设计基础》实现mypwd
2017-2018-1 20155327 <信息安全系统设计基础>实现mypwd Linux pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. p ...
- 北京Uber优步司机奖励政策(4月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Hadoop NameNode HA 和 ResourceManager HA
1.集群规划 1.1 规划说明 hadoop1 cluster1 nameNode hadoop2 cluster1 nameNodeStandby ZooKeeper ResourceManager ...
- 关于ExecuteNonQuery执行的返回值(SQL语句、存储过程)
因为msdn中说返回受影响的行数: Executes a Transact-SQL statement against the connection and returns the number of ...
- python简介、第一个python程序、变量、字符编码、用户交互程序、if...else、while、for
也愿大家永葆初心-- 已识乾坤大,犹怜草木青. 一.python简介 首先,我们普及一下编程语言的基础知识.用任何编程语言来开发程序,都是为了让计算机干活,比如下载一个MP3,编写一个文档等等,而计算 ...
- java.lang.Boolean.valueOf(String s)
简单说,就是s为true(这四个字母大小写任意)时,返回值为true,否则为false public class one { public static void main(String[] args ...
- Unity编辑器扩展 Chapter3--Create Custom Inspector
一.Create Custom Inspector 重绘inspector面板一方面是我们的挂在脚本的窗口变得友好,另一方面可以让其变得更强大,比如添加一些有效性验证. 二.重要说明 1.Editor ...
- python序列和其它类型的比较
序列对象可以与相同类型的其他对象比较.它们使用 字典顺序 进行比较:首先比较两个python序列的第一个元素,如果不同,那么这就决定了比较操作的结果.如果它们相同,就再比较每个序列的第二个元素,以此类 ...