【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, ...
随机推荐
- ERROR in index.web.js from UglifyJs
使用weexpack构建weex应用时,npm run serve一直报这个错误 ERROR in index.web.js from UglifyJs Unexpected token: name ...
- ZooKeeper启动报错 JAVA_HOME is incorrectly set
解决办法:在zkEnv.cmd文件中直接写死调用的jdk路径 set JAVA_HOME="D:\Program Files\Java7\jdk1.7.0_51" if not e ...
- Mockito单测,mock service层的mapper
转载:https://blog.csdn.net/paincupid/article/details/53561435 1.引入mockito jar包 <dependency> < ...
- [海蜘蛛] 海蜘蛛 V8 全线无限试用版 免费发布破解教程
http://bbs.p52.cn/forum.php?mod=viewthread&tid=3499&extra=page%3D1&page=1&_dsign=79c ...
- 被查封7周之后,全球最大BT网站“海盗湾”又重新活过来了【36kr】
原文地址 原文地址 "免费"和"版权","自由"和"监管"永远在较量啊,矛盾共同体,事物的两面性~ 被查封7周之后,全球 ...
- 去除console.log()打印语句
打印语句:console.log() ,一句话描述它! “用的时候感觉贼爽,不用的时候脑袋痛吧?” 以下提供三种解决方案: 一. webpack打包时去除,适合Vue项目 二. vscode正则匹配, ...
- Windows下的Qt Creator的安装
采用Qt和Qt creator分别下载和安装的方式:(需要手动设置关联Qt和Qt Creator) 一.软件下载 从http://qt-project.org/downloads分别下载Qt和Qt ...
- pynput使用简单说明
控制鼠标 from pynput.mouse import Button, Controller import time mouse = Controller() print(mouse.positi ...
- sell 创建项目
1.数据操作软件 2.创建项目 3.mevan 镜像
- 转-ubuntu清理卸载wine的残余项目
背景:前段时间,装了wine试用了一下,感觉实在没啥意思就卸载了.但是卸载以后发现还有些尾巴碍眼,如打开文件时右键菜单里就会有“使用notepad打开”的选项,虽然没有什么别的问题,但是看着碍眼.所以 ...