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 4786(最小生成树 kruskal)
题目链接:pid=4786" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4786 Prob ...
- web.xml中的ServletContextListener
要想了解ServletContextListener,先看看web.xml中的<listener>配置. 一)web.xml中的内容载入顺序: 首先能够肯定的是,载入顺序与它们在 web. ...
- (WPF)附加属性
<Window x:Class="DeepXAML.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...
- How can I pass data from Flask to JavaScript in a template?
https://stackoverflow.com/questions/11178426/how-can-i-pass-data-from-flask-to-javascript-in-a-templ ...
- Django 使用UEditor
Django package 的一些包不支持upload file, 而且 有几个支持的不是收费的就是要开csrf ,这对于苦逼程序猿来说始终是件恼火的事.所以经过查阅各种资料.看了各种各样的配置do ...
- jquery中一些容易忽略的方法
jquery.ajaxPrefilter:([dataTypes],handle(options,originalOptions,jqXHR)):在发送请求之前和执行$.ajax之前操作options ...
- RK3288以太网的mac地址调试笔记【学习笔记】【原创】
平台信息:内核:linux3.1.0系统:android/android6.0平台:RK3288 作者:庄泽彬(欢迎转载,请注明作者) 邮箱:2760715357@qq.com 说明:提供以太网mac ...
- 日元兑换——国内兑换需要护照和签证,国外的机场有兑换ATM
在中国换日元:在中国的商业银行都可以换取日元,但是换汇者必须持有护照.签证等材料.换汇的汇率是按照即时汇率进行结算,如是现钞则按钞买价兑换,另外还要收取0.5%的手续费. 在日本换日元:除了在日本银行 ...
- iphone设备尺寸规格
1.以下是iphone各种设备的尺寸规格 2.开发时只需要按“逻辑分辨率”来,1x,2x,3x主要用于切图时按不同大小来切图,如1x的图就是按照“逻辑分辨率”大小的效果图切出来的原图,2x就是1x原图 ...
- c# 读取内存
C# 用内存映射文件读取大文件(.txt) 网上有好多这类的文章,大部分都是用C/C++写的,也有部分C#写的,都思想都是一样的,调用win32 API. 至于什么是内存映射文件,相信还是有好多人 ...