【刷题-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 the kth largest element in the sorted order, not the kth distinct element.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
解法1 直接调用sort()
函数,返回第k大的数字
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
}
解法2 用quick_sort的思路
解法2.1 自己写partition函数,为了避免1 vs n-1的划分导致的性能下降,可以采用random_partition
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
return search(nums, k, 0, nums.size() - 1);
}
int search(vector<int>& nums, int k, int l, int r){
int order = random_partition(nums, l, r);
if(order == k)return nums[l+order-1];
else if(order < k){
return search(nums, k - order, l+order, r);
}else{
return search(nums, k, l, l + order - 2);
}
}
int random_partition(vector<int>& nums, int l, int r){
srand((unsigned)time(NULL));
int idx = rand() % (r-l+1)+ l;
swap(nums[l], nums[idx]);
return partition(nums, l, r);
}
int partition(vector<int>& nums, int l, int r){
int pivot = nums[l];
int i = l, j = r;
while(i < j){
while(nums[j] < pivot && j > i)j--; // 注意不要丢掉i < j的条件
nums[i] = nums[j];
while(nums[i] >= pivot && i < j)i++; // 注意不要丢掉i < j的条件
nums[j] = nums[i];
}
nums[i] = pivot;
return i - l + 1;
}
};
解法2.2 调用stl中的partition函数。原型:
iterator partition(nums.begin(), nums.end(), cond)
,其中cond
是一个函数,满足cond
条件的元素会被放到前一段,不满足的放到后一段
static int pivot;
static bool cmp(int x){
if(x >= pivot)return true;
else return false;
}
int random_partition(vector<int>& nums, int l, int r){
srand((unsigned)time(NULL));
int idx = rand() % (r-l+1)+ l;
swap(nums[l], nums[idx]);
pivot = nums[l];
auto it = partition(nums.begin() + l, nums.begin() + r + 1, cmp);
return it - nums.begin();
}
【刷题-LeetCode】215. Kth Largest Element in an Array的更多相关文章
- 剑指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, ...
- 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 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Java for 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
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 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- C#版 - Leetcode 215. Kth Largest Element in an Array-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 ...
随机推荐
- CF94A Restoring Password 题解
Content 有一个 \(80\) 位的 \(01\) 字符串,由 \(8\) 个长度为 \(10\) 的 \(01\) 字符串组成,每个小字符串分别对应一个数字.现在,给出这个字符串和 \(0\) ...
- AT2664 [AGC017A] Biscuits 题解
Content 有一个长度为 \(n\) 的数列 \(a\).你希望从中选出一些数,使得这些数的和对 \(2\) 取模后的结果为 \(P\).求方案数. 数据范围:\(1\leqslant n\leq ...
- SpringBoot使用@Async实现异步调用
1.@EnableAsync 首先,我们需要在启动类上添加 @EnableAsync 注解来声明开启异步方法. @SpringBootApplication @EnableAsync public ...
- 【LeetCode】259. 3Sum Smaller 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...
- 【九度OJ】题目1107:搬水果 解题报告
[九度OJ]题目1107:搬水果 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1107 题目描述: 在一个果园里,小明已经将所有的水 ...
- 【九度OJ】题目1012:畅通工程 解题报告
[九度OJ]题目1012:畅通工程 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1012 题目描述: 某省调查城镇交通状况 ...
- 【九度OJ】题目1199:找位置 解题报告
[九度OJ]题目1199:找位置 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1199 题目描述: 对给定的一个字符串,找出有重复的 ...
- 【剑指Offer】二叉树中和为某一值的路径 解题报告(Python)
[剑指Offer]二叉树中和为某一值的路径 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-intervi ...
- 【剑指Offer】构建乘积数组 解题报告(Python)
[剑指Offer]构建乘积数组 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews 题目 ...
- CapstoneCS5212替代RTD2166|DP转VGA转换电路设计方法|CS5212替代方案
Capstone CS5212适用于设计DP转VGA转换电路,主要用在嵌入式单片机基于工业机或者INTEL X86主板上面,也适用于多个电子配件市场和显示器应用程序,如笔记本电脑.主板.台式机.适配器 ...