215 Kth Largest Element in an Array 数组中的第K个最大元素
在未排序的数组中找到第 k 个最大的元素。请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素。
例如,
给出 [3,2,1,5,6,4] 和 k = 2,返回 5。
注意事项:
你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度。
详见:https://leetcode.com/problems/kth-largest-element-in-an-array/description/
Java实现:
方法一:
class Solution {
public int findKthLargest(int[] nums, int k) {
int n=nums.length;
if(n<k||nums==null){
return -1;
}
int low=0;
int high=n-1;
int m=n-k;
while(true){
int index=partition(nums,low,high);
if(index==m){
return nums[index];
}else if(index>m){
high=index-1;
index=partition(nums,low,high);
}else{
low=index+1;
index=partition(nums,low,high);
}
}
}
private int partition(int[] nums,int low,int high){
int pivot=nums[low];
while(low<high){
while(low<high&&nums[high]>=pivot){
--high;
}
nums[low]=nums[high];
while(low<high&&nums[low]<=pivot){
++low;
}
nums[high]=nums[low];
}
nums[low]=pivot;
return low;
}
}
方法二:
class Solution {
public int findKthLargest(int[] nums, int k) {
int n=nums.length;
if(n<k||nums==null){
return -1;
}
PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();
for(int i=0;i<n;++i){
if(i<k){
minHeap.offer(nums[i]);
}else if(minHeap.peek()<nums[i]){
minHeap.poll();
minHeap.offer(nums[i]);
}
}
return minHeap.peek();
}
}
C++实现:
方法一:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int,vector<int>,greater<int>> minH;
for(int i=0;i<nums.size();++i)
{
if(minH.size()<k)
{
minH.push(nums[i]);
}
else
{
if(minH.top()<nums[i])
{
minH.pop();
minH.push(nums[i]);
}
}
}
return minH.top();
}
};
方法二:
class Solution {
public:
int partition(vector<int>& nums, int left, int right) {
int pivot = nums[left];
int l = left + 1, 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;
}
int findKthLargest(vector<int>& nums, int k) {
int left = 0, right = nums.size() - 1;
while (true) {
int pos = partition(nums, left, right);
if (pos == k - 1) return nums[pos];
if (pos > k - 1) right = pos - 1;
else left = pos + 1;
}
}
};
参考:https://www.cnblogs.com/grandyang/p/4539757.html
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]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 a Stream 数据流中的第K大的元素
Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...
- [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 ...
- 剑指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, ...
- 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 ...
- 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 (2 solutions)
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
随机推荐
- hdu 5303 Delicious Apples
这道题贪心 背包 假设在走半圆之内能够装满,那么一定优于绕一圈回到起点.所以我们从中点将这个分开,那么对于每一个区间由于苹果数非常少,所以能够利用pos[x]数组记录每一个苹果所在的苹果树位置,然后将 ...
- Python常用的几种常用的内置函数
abs(x) 用于返回绝对值 divmod(x,y) 函数中传入两个数字,返回的是x/y的一个结果的元组(商,余数) pow(x,y) 用于 ...
- python各进制、字节串间的转换
>>> i = 13 >>> bin(i) '0b1101' >>> oct(i) '0o15' >>> hex(i) '0xd ...
- pip 安装速度慢解决办法
https://blog.csdn.net/liujingclan/article/details/50176597 https://blog.csdn.net/rytyy/article/detai ...
- linux 【目录】
[第一篇]linux[目录] [第五篇]特殊权限及定时任务 [第六篇]用户和用户管理及定时任务复习
- tomcat项目重复加载问题
主要是通过配置<Tomcat安装目录>/conf/server.xml文件 步骤: 1.打开server.xml,在</Host>的上一行添加内容格式如下 <Contex ...
- npm学习笔记
NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...
- maven工具使用
一.工具安装: 所需工具 : JDK 1.8Maven 3.3.3 1.安装JDK 和 JAVA_HOME 2.添加 M2_HOME 和 MAVEN_HOME 3.添加到环境变量 - PATH 4.验 ...
- redhat修复hostname主机名
1.修改文件vi /etc/sysconfig/network下的hostname,如: NETWORKING=yes HOSTNAME=master 2.修改文件:vi /etc/hosts 127 ...
- luogu4917天守阁的地板
https://www.zybuluo.com/ysner/note/1317548--- 题面 给出\(n\),用所有长为\(a\).宽为\(b\)\((1\leq a,b\leq n)\)的长方形 ...