LeetCode Kth Largest Element in an Array (快速排序)
题意:
在一个无序的数组中第k大的数是多少?
思路:
按照快排的思路,如果每次分成两段后,设为L和R。如果R>=k ,则答案在右边集合,否则在左边集合。
这里用了3位取中法。注意快排别给写死循环了。
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
if(k>nums.size()) return ;
return DFS(nums,,nums.size()-,k);
} int DFS(vector<int>& nums,int s,int e,int k)
{
int L=s, R=e;
//三位取中法
if(nums[e]>nums[s]) swap(nums[s],nums[e]);
if(nums[s]>nums[(s+e)/]) swap(nums[s],nums[(s+e)/]); int mid=nums[s];
while(L<R)
{
while(L<R && nums[R]>=mid) R--; //找小
nums[L]=nums[R];
while(L<R && nums[L]<=mid) L++; //找大
nums[R]=nums[L];
}
nums[L]=mid;
int len=e-L;//右边部分的元素个数
if(len+==k) return mid;
if(len>=k) return DFS(nums,L+,e,k);
else return DFS(nums,s,L-,k-len-);
}
};
AC代码
LeetCode Kth Largest Element in an Array (快速排序)的更多相关文章
- [LeetCode] 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 ...
- LeetCode Kth Largest Element in an Array
原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...
- LeetCode——Kth Largest Element in an Array
Description: Find the kth largest element in an unsorted array. Note that it is the kth largest elem ...
- 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面试准备: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 Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- 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 ...
随机推荐
- Objective-C( 语法一)
点语法 点语法的本质是方法调用 成员变量的作用域 @public : 在任何地方都能直接访问对象的成员变量 @private : 只能在当前类的对象方法中直接访问(@implementation中默认 ...
- Installing a single-server IBM FileNet P8 Platform system with IBM Content Navigator
Product documentation Abstract You can install all of the IBM FileNet P8 Platform components on a si ...
- Python Twisted介绍
原文链接:http://www.aosabook.org/en/twisted.html 作者:Jessica McKellar Twisted是用Python实现的基于事件驱动的网络引擎框架.Twi ...
- [转]使用maven镜像
综述 用maven做项目,最郁闷的莫过于某些依赖库下载不了.被墙了,你懂的.使用maven镜像仓库及其重要,特别是国内的镜像,可以有效缓解被墙疼痛. 常用的镜像 国外镜像 ibiblio.org &l ...
- mouseleave 与 mouseout 的不同
Q:给某div添加mouseout事件后,在空白区域移动到其子元素(如按钮)上(此时并没有离开此div)时,会触发mouseout事件,而mouseleave则不会 A:与 mouseout 事件不同 ...
- python错误集锦
1.lt与list等同,不能作为变量名 2.中文路径名:os.path.normcase(filepath) 如果遇到 ascii码在路径某处不能转换, 那么 filepath.encode('gbk ...
- 二模 (5)day1
第一题: 题目大意:解一元一次方程(只有+-符号): 解题过程:直接处理处两边的x的系数和常数项,字符串的处理即可. 第二题: 题目大意:求逆序对数. 解题过程:直接归并排序. 第三题: 题目大意:多 ...
- batch insert 1 million datas into mysql
最近尝试插入1百万条数据进db,以mysql为例. 1. 顺序insert 先写了个无脑的for循环作为base-line,插1万条耗时1m53s,根本不敢插1百万. foreach(var stud ...
- IT公司100题-8-智力题
问题1: 有两个房间,一间房里有三盏灯,另一间房有控制着三盏灯的三个开关, 这两个房间是分割开的,从一间里不能看到另一间的情况. 现在要求受训者分别进这两房间一次,然后判断出这三盏灯分别是由哪个开关控 ...
- 调整label中text显示的行间距
调整label中text显示的行间距最近再做一个项目时,发现UILabel中text的系统默认行间距不能满足要求,于是在网上找到了调整行间距的代码.跟大家分享一下,希望能对你有所帮助.悦德财富:htt ...