【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 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.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解法一:
先排序,复杂度O(nlogn)
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size()-k];
}
};

解法二:
建最大堆,取k次最大元素。复杂度O(n+klogn)
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
make_heap(nums.begin(), nums.end());
int ret;
int n = nums.size();
while(k --)
{
pop_heap(nums.begin(), nums.begin()+n);
ret = nums[n-];
n --;
}
return ret;
}
};

【LeetCode】215. Kth Largest Element in an Array (2 solutions)的更多相关文章
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- 【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 so ...
- 【刷题-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 ...
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
随机推荐
- Tensorflow动态seq2seq使用总结(r1.3)
https://www.jianshu.com/p/c0c5f1bdbb88 动机 其实差不多半年之前就想吐槽Tensorflow的seq2seq了(后面博主去干了些别的事情),官方的代码已经抛弃原来 ...
- centos:时间同步
转自:https://blog.csdn.net/u011391839/article/details/62892020 Linux的时间分为System Clock(系统时间)和Real Time ...
- 【机器学习】Logistic Regression 的前世今生(理论篇)
Logistic Regression 的前世今生(理论篇) 本博客仅为作者记录笔记之用,不免有非常多细节不正确之处. 还望各位看官能够见谅,欢迎批评指正. 博客虽水,然亦博主之苦劳也. 如需转载,请 ...
- nod32的内网在线更新设置
需要一个iis地址,最好能够目录浏览,权限够大. 还必须: 增加一个MIME类型,.ver,类型填写application/x-ver
- Sublime Es6教程2-基本语法
2.基本语法 let, const, forEach,for of class, extends, super arrow functions, template string, destructur ...
- 前端html用一个表单来映射后台多个对象
public class entity1 { private String id; public String getId() { return id; } public void setId(Str ...
- 启动IntelliJ IDEA 2016报错:cannot start under Java 1.7 : Java 1.8 or later is required 解决办法
idea64.exe启动错误:Cannot start under Java 1.7.0xxx IntelliJ IDEA : Unsupported java version Cannot star ...
- 渗透测试工具SPARTA介绍
0x01 sparta安装 kali系统默认安装了sparta 需要基础环境: git clone https://github.com/secforce/sparta.git apt-get ins ...
- Go语言中Socket通信之Tcp客户端
1.用法: (1)定义远程IP地址.使用net.ResolveTCPAddr()方法,定义一个TCP地址,做为目标连接地址. (2)调用net.DialTCP("tcp",nil, ...
- Spark的运行模式(2)--Yarn-Cluster和Yarn-Client
3. Yarn-Cluster Yarn是一种统一资源管理机制,可以在上面运行多种计算框架.Spark on Yarn模式分为两种:Yarn-Cluster和Yarn-Client,前者Driver运 ...