在未排序的数组中找到第 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个最大元素的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  6. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. jni——如何转换有符号与无符号数

    java数据结构默认均为有符号数,而通过jni转换到c/c++层,却不一定是有符号数. 如若在java中存储的即为无符号数,则在jni中可将jbyte直接进行类型转换. 若进行操作,则可在计算时,先将 ...

  2. 剑指Offer面试题15(Java版):链表中倒数第K个结点

    题目: 输入一个链表.输出该链表中倒数第k哥结点.  为了符合大多数人的习惯,本题从1開始计数.即链表的尾结点是倒数第1个结点. 比如一个链表有6个结点.从头结点開始它们的值依次是1.2.3,4,5, ...

  3. Java使用三种不同循环结构对1+2+3+...+100 求和

    ▷//第一种求法,使用while结构 /** * @author 9527 * @since 19/6/20 */ public class Gaosi { public static void ma ...

  4. SSH常见错误

    错误一: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml] ...

  5. hdfs对namenode format 之后 应该首先检查内存消耗情况,以判断是否支持开启yarn

    http://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-common/yarn-default.xml  3.0.0 yarn.sc ...

  6. mktemp temp race attack 临时文件隐患

    /tmp  安全隐患 -/tmp   在家目录  程序目录下 创建 临时文件

  7. Buildroot 龙芯1C支持指南

    本文转载自:https://github.com/pengphei/smartloong-sphinx/blob/master/source/cn/loongson1c_buildroot_guide ...

  8. YTU 2895: H--唱歌的鸟儿

    2895: H--唱歌的鸟儿 时间限制: 1 Sec  内存限制: 128 MB 提交: 26  解决: 10 题目描述 烟大东门有一棵大杨树,树上经常会有很多鸟儿飞来飞去.春天来了,学生物的小姜发现 ...

  9. 使用C#开发HTTP服务器系列之Hello World

    各位朋友大家好,我是秦元培,欢迎大家关注我的博客.从今天起,我将开始撰写一组关于HTTP服务器开发的系列文章.我为什么会有这样的想法呢?因为人们对Web技术存在误解,认为网站开发是Web技术的全部.其 ...

  10. Violet蒲公英

    传送门 题目要求求出给定区间内编号最小的众数,强制在线. 虽然说这是个黑题……不过我们可以用暴力分块解决它.首先先对所有数离散化,这个不影响众数.我们先预处理出每个数在前i个块内出现了多少次,再预处理 ...