【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函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
随机推荐
- Python-条件语句和循环语句
·条件语句 笔记: If 布尔值: print(‘hello,world!’) 当表达式为布尔表达式时,Flase None 0 ”” () [] {} 都视为假! @ ...
- 20155212 实验二 Java面向对象程序设计
20155212 实验二 Java面向对象程序设计 单元测试 三种代码 用编程去解决问题,必须学会写三种码: 伪代码 产品代码 测试代码 例: MyUtil 类解决一个百分制成绩转成"优.良 ...
- 一篇文章帮你梳理清楚API设计时需要考虑的几个关键点
本文作者是Enchant的架构师,他最近研究了Netflix.SoundCloud.谷歌.亚马逊.Spotify等公司的微服务实践,并根据自己的理解总结出了一套适用于现代Web和云技术的微服务实战经验 ...
- L013-linux基础正则表达式手把手实战讲解小节
L013-linux基础正则表达式手把手实战讲解小节 这么一看又有10天没更新博客了,最近也一直在学就是时间比较闲散,再加上做上次老师留的十多道题,所以时间比较紧张,本来做完题准备直接先看L014讲解 ...
- LDPC译码算法代码概述
程序说明 V0.0 2015/1/24 LDPC译码算法代码概述 概述 本文介绍了包括LDPC_Simulation.m, ldpcdecoderbp1.m,ldpcdecoderminsum ...
- Firefox开发
官方文档 First extension 目录结构 ➜ firefox tree . └── borderify └── manifest.json // 必须 directory, files ma ...
- 爬虫2.3-scrapy框架-post、shell、验证码
目录 scrapy框架-post请求和shell 1. post请求 2. scrapy shell 3. 验证码识别 scrapy框架-post请求和shell 1. post请求 scrapy框架 ...
- C语言—栈
栈的操作:进栈和出栈 #include "stdafx.h" #include "stack.h" #define maxsize 20 typedef int ...
- Golang项目开发管理
工具 1. task(项目管理,类似于make) go get -u -v github.com/go-task/task/cmd/task 2. gopm(go依赖管理) go get -u git ...
- python基础知识-7-内存、深浅、文件操作
python其他知识目录 1.一些对内存深入理解的案例 以下列举列表,列表/字典/集合这些可变类型都是一样的原理 变量是个地址,指向存储数据的内存空间的地址,它的实质就相当于c语言里的指针.变量和数据 ...