leetcode解题报告(31):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.
分析
代码如下:
class Solution {
public:
int partition(vector<int>&nums,int p,int r)
{
int x = nums[r];
int i = p - 1;
for(int j = p; j != r; ++j)
if(nums[j] < x)
swap(nums[++i],nums[j]);
swap(nums[i + 1],nums[r]);
return i + 1;
}
int findKthLargest(vector<int>& nums, int k) {
/*
sort(nums.begin(),nums.end());
return nums[nums.size() - k];
*/
int p = 0,r = nums.size() - 1;
while(true){
int q = partition(nums,p,r);
if(q < nums.size() - k)
p = q + 1;
else if(q > nums.size() - k)
r = q - 1;
else
return nums[q];
}
}
};
leetcode解题报告(31):Kth Largest Element in an Array的更多相关文章
- 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 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- Leetcode 之 Kth Largest Element in an Array
636.Kth Largest Element in an Array 1.Problem Find the kth largest element in an unsorted array. Not ...
- 网易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 ...
- 【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 ...
- 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
Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...
随机推荐
- 精确选择识别png图片有像素的区域(使用方法)
/** * * *---------------------------------------* * | ***精确选择识别png图片有像素的区域*** | * *----------------- ...
- ssh密码正确无法连接
新建了一个虚拟机,准备用ssh 连接管理,提示权限不足, 账号密码没问题,防火墙没有开 打开ssh服务的配置文件,默认在 /etc/ssh/sshd_config (不是 ssh_config) 检 ...
- delphi indy Idhttp error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert protocol version
在使用 indy 中的 idhttp 组件访问 https 网站时,出现如下错误: error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert pr ...
- 解决springboot 新版本 2.1.6 spring-boot-starter-actuator 访问报404
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- 一步一步手写GIS开源项目-(1)500行代码实现基础GIS展示功能
1.开篇 大学毕业工作已经两年了,上学那会就很想研读一份开源GIS的源码,苦于自己知识和理解有限,而市面上也没有什么由浅入深讲解开源gis原理的书籍,大多都是开源项目简介以及项目的简单应用.对于初级程 ...
- Non-Maximum Suppression(非极大值抑制)
定义与介绍(NMS 以及soft-NMS也有简单的介绍): https://www.cnblogs.com/makefile/p/nms.html IoU的介绍这篇写的不错: https://oldp ...
- node-red File读取好保存
File节点是操作文件的节点 file文件的保存 拖拽 注入节点inject file节点(writes msg.payload to a file)和 debug节点到工作区,并连线 设置file ...
- java之初识hibernate
1. 使用jdbc进行数据库操作:获取数据库连接,编写sql语句,执行sql操作,关闭连接. 比如:每次创建连接,释放资源----使的执行效率降低: 解决方案:连接池. 编写sql语句动作----简单 ...
- windows下pyinstaller打包踩坑记录
示例: 需要打包的是 ReadConfig.py 文件,同文件夹下调用了Interface.py文件,ui文件夹下调用了 Ui_config.py和Ui_Error.py文件,Interface.py ...
- idea全局护眼色绿豆沙
1.settings->plugins->BackgroundImage 2.在导航栏选择Help->FindAction 3.set background image 4.选择图片 ...