【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 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 findKthLargest(vector<int>& nums, int k) {
vector<int> leftvec;
vector<int> rightvec;
//以nums[0]作为分界元素,从nums[1]开始遍历
//比nums[0]小的元素放入leftvec,反之放入rightvec
for(int i = ; i < nums.size(); i++)
{
if(nums[i] <= nums[])
leftvec.push_back(nums[i]);
else
rightvec.push_back(nums[i]);
}
//rightvec的长度即为大于nums[0]的元素的个数
int len = rightvec.size();
if(len >= k)//前k大的元素都在rightvec中,递归求解rightvec
{
return findKthLargest(rightvec, k);
}
else if(len == k - )//比nums[0]大的元素有k-1个,那么nums[0]即为所求结果
{
return nums[];
}
else//比nums[0]大的元素有[0, k-2]个,说明第K大的元素在leftvec中,递归求解leftvec
{
return findKthLargest(leftvec, k - len - );
}
}
};
【LeetCode 215】Kth Largest Element in an Array的更多相关文章
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- 【leetcode】Kth Largest Element in an Array (middle)☆
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
题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
- 【LeetCode 230】Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 【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 ...
- 【刷题-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 ...
- 剑指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 ...
随机推荐
- 获取SqlDataReader的列名
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionSt ...
- emms指令在MMX指令中的作用
emms指令在MMX指令中的作用 转自:http://blog.csdn.net/psusong/archive/2009/01/08/3737047.aspx MMX和SSE都是INTEL开发的基于 ...
- HorseCome
紫气东来,祝福也随之而来,喜鹊登梅,福禄也登上眉梢,马年将至,喜庆将萦绕身旁,在这个美好的日子送上我最真挚的祝福,祝身体安康. 春晓,春晓,处处绿杨芳草.山山水水,欢欢笑笑,共祝六合同春,步步登高!
- 查看服务器的TCP/IP(http)连接情况
6.那具体到,怎样监听一个具体程序的TCP/IP连接情况呢???!! 就要用:netstat -n|grep 80 命令来查看具体端口对应程序的TCP/IP连接情况了 netstat -n|grep ...
- 如何删除ArcSde Service服务
1)打开“控制面板”,“服务”,找到“ArcSde Service(somename)”,这里somename就是你的ArcSde服务的真实的名字,记住这个名字(为叙述方便,以下用somename表示 ...
- android actionbar标题栏
在android的actionBar中,actionBar的视图是固定的,左边是程序的图标和title,右边是添加的menuItem,如果想要定制actionbar中的view就要自定义视图. 首先要 ...
- Setup Oracle 11gR2 for Redhat Linux AS 4 Update 7 x64
Setup Oracle 11gR2 for Redhat Linux AS 4 Update 7 x64 1. checking linux version. [root@localhost ~]# ...
- R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties
Warning message: In ks.test(x, y) : p-value will be approximate in the presence of ties The warnin ...
- Android开发之单例模式
参考:http://blog.csdn.net/guolin_blog/article/details/8860649 http://www.cnblogs.com/liyiran/p/5283690 ...
- Oracle存储过程格式
create or replace procedure sp_test ( -- 此地写传入的值 v_tjfs varchar2, --不用申明长度 v_kssj varchar2, v_ret ou ...