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 ...
随机推荐
- go 学习笔记(2)go test
Test 的写法: 每一个test文件必须import 一个"testing" test文件下的每一个test case均必须以Test开头并且符合TestXxx形式,否则go t ...
- Python之TensorFlow的(案例)验证码识别-6
一.这里的案例相对比较简单,主要就是通过学习验证码的识别来认识深度学习中我们一般在工作中,需要处理的东西会存在哪些东西. 二.因为我没有数据集,没有关系,这里自己写了一个数据集,来做测试,为了方便我把 ...
- Visual Studio 2019 使用.Net Core 3.0 一
一.前言 早在很久之前微软便公布 .NET Core 3.0 将支持开发Winform应用程序等等新特性,现如今 .NET Core 3.0 预览版已经出来第八个预览版了,从 .NET Core 2. ...
- NEST 多IndexType与分页
/// <summary> /// POST /_all/employee/_search?typed_keys=true /// </summary> public void ...
- Twitter分布式自增ID算法snowflake原理解析(Long类型)
Twitter分布式自增ID算法snowflake,生成的是Long类型的id,一个Long类型占8个字节,每个字节占8比特,也就是说一个Long类型占64个比特(0和1). 那么一个Long类型的6 ...
- Objective-C学习笔记 利用协议实现回调函数
来源:http://mobile.51cto.com/iphone-278354.htm Objective-C学习笔记 利用协议实现回调函数是本文要介绍的内容,主要是实现一个显示文字为测试的视图,然 ...
- Linux 内核/驱动开发总结
总体来看,需要一个阶段性总结了,因为现在SD卡的调试也进入了卡壳期.大概会出一系列的总结文章,主要涉及的主题在下面列出: 1.开发工具:gcc/gdb/vim/ctags 2.Makefile和KCo ...
- vue-element-admin 前端框架 使用感触
感触: 不搜不知道,一搜吓一跳!经常百度很重要. 美国有gitgub:https://github.com/search?q=vue-element-admin 中国有码云:https://gitee ...
- Delphi INI 文件读写
delphi中,配置文件的相关操作. () INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个节点又允许有多个关键字, “=”后面是该关键 ...
- mysql基础指令知识
桌面指令(cmd)进入mysql客户端 第一步:安装mysql,配置环境变量 第二步:手动开启服务 第三步:输入如下指令: mysql [-h localhost -P 3306] -u 用户名 - ...