【刷题-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 ...
随机推荐
- java 图形化小工具Abstract Window Toolit ;布局管理器FlowLayout流式布局;BorderLayout边界布局;GridLayout网格布局;CardLayou重叠卡片布局;BoxLayout方框布局;绝对定位
1.FlowLayout流式布局管理器: FlowLayout布局管理器中,组件像水流一样向某方向流动(排列),遇到障碍(边界)就折回,重头开始排列 .在默认情况下,FlowLayout局管理器从左向 ...
- call this的范围
var f1=function(){this.a="类f1的实例的a属性"}; f1代表一个类: f1.a='对象f1的a属性'; var f2=function(){};//类f ...
- JAVA获取指定日期的一天的开始时刻(时间)和结束时刻(时间)
注: SimpleDateFormat是线程不安全的 public static SimpleDateFormat format = new SimpleDateFormat("yyyyMM ...
- LeetCode1237找出给定方程的正整数解
题目 给定方程f和值z,找出给定方程f(x,y)=z的正整数解x,y.f(x,y)关于x.y都是严格单调的. 题目保证 f(x, y) == z 的解处于 1 <= x, y <= 100 ...
- 一站式元数据治理平台——Datahub入门宝典
随着数字化转型的工作推进,数据治理的工作已经被越来越多的公司提上了日程.作为新一代的元数据管理平台,Datahub在近一年的时间里发展迅猛,大有取代老牌元数据管理工具Atlas之势.国内Datahub ...
- python 脚本或者flask 注册成为windows服务
分享下脚本注册成为win服务 这个借鉴了其他大佬得 具体是谁忘记了有机会推荐 今天也是找了之前得改出来得 首先安装三个模块 最后一个是定时任务如果不需要可以不装 pip install psutil ...
- Linux 使用 tail 命令查看文件内容
使用方法: $ tail --help 用法:tail [选项]... [文件]... 以标准输出的形式打印文件的最后10行内容: 如果不指定文件,或者文件为"-",则从标准输入中 ...
- 我踩过的Django的坑
把自己在使用Django过程中吃过的亏 踩过的坑 记录一下 1. unique_together传的是元组 正确:unique_together = (('app_id', 'module_id', ...
- 获取python的版本
import sys # Syntax sugar. _ver = sys.version_info # 获取python版本 #: Python 2.x? is_py2 = (_ver[0] == ...
- 关闭SpringBoot logo图标
public static void main(String[] args) {// SpringApplication.run(LicenseApp.class, args); //关闭Spring ...