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 ...
随机推荐
- Visual Studio VS如何切换代码自动换行
工具-选项-文本编辑器-自动换行
- vue - 官方 - 上手
Vue和其它框架一样,有用CDN或本地JavaScript框架,国内我推荐 bootstrap cdn. 为什么很多人选择CDN呢? CDN:内容分发网络(不同区域不同服务器,更快),减少本地服务器压 ...
- hdu 5087 Revenge of LIS II ( LIS ,第二长子序列)
链接:hdu 5087 题意:求第二大的最长升序子序列 分析:这里的第二大指的是,全部的递增子序列的长度(包含相等的), 从大到小排序后.排在第二的长度 cid=546" style=&qu ...
- redux-saga 异步流
前言 React的作用View层次的前端框架,自然少不了很多中间件(Redux Middleware)做数据处理, 而redux-saga就是其中之一,目前这个中间件在网上的资料还是比较少,估计应用的 ...
- python实现接口自动化
一.总述 Postman:功能强大,界面好看响应格式自主选择,缺点支持的协议单一且不能数据分离,比较麻烦的还有不是所有的公司都能上谷歌SoupUI:支持多协议(http\soup\rest等),能实现 ...
- Python函数参数传递
Python中函数参数的传递是采用传值方式,但是和C/C++有所不同 C/C++方式 void fun(int a) { a = 10; } void main() { int c =3; fun(c ...
- 基于docker容器搭建fastdfs分布式文件系统
本次环境的搭建参考了 https://blog.csdn.net/qq_43455410/article/details/84797814, 感谢博主. 主要流程如下: 1. 下载fastdfs镜像 ...
- Enterprise Architect 生成项目类图
Enterprise Architect使用教程: https://blog.csdn.net/chenglc1612/article/details/81083151 主要流程 --到此-自动生成完 ...
- ubuntu LNMP环境下安装Redis,以及php的redis扩展
1.下载 sudo wget http://download.redis.io/releases/redis-4.0.9.tar.gz 2.解压 sudo tar zvxf redis-4.0.9.t ...
- python名片管理系统V2
主程序: #! /usr/bin env python3 # -*- coding: utf-8 -*- # 项目三: # 1.要求:编写一个名片管理系统,功能如下: # 用户输入相对应的指令,实现对 ...