网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数。
给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。
[1,3,5,2,2],5,3
返回:2
note:
注意手写快排的时候:
while(i < j) {
while(j > i && a[j] > a[left]) j--;
while(i < j && a[i] <= a[left]) i++;
if(i < j) {
swap(a[i],a[j]);
}
}
while(j > i && a[j] > a[left]) j--;
while(i < j && a[i] <= a[left]) i++;
这两个顺序不能反了,不然下标会错位一个
class Finder {
public:
int findKth(vector<int> a, int n, int K) {
// write code here
return quickfind(a,,n - ,K);
}
int quickfind(vector<int> &a,int left,int right,int K) {
int i = left,j = right;
while(i < j) {
while(j > i && a[j] > a[left]) j--;
while(i < j && a[i] <= a[left]) i++;
if(i < j) {
swap(a[i],a[j]);
}
}
swap(a[left],a[i]);
int dis = right - i + ;
if(dis == K){
return a[i];
}
else if(K < dis) {
return quickfind(a,i + ,right,K);
}
else{
return quickfind(a,left,i - ,K - dis);
}
}
};
--------------------------------------------------
leetcode:
215. Kth Largest Element in an Array
- Total Accepted: 67793
- Total Submissions: 195182
- Difficulty: Medium
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.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
return quickfind(nums,,nums.size() - ,k);
}
int quickfind(vector<int> &a,int left,int right,int K) {
int i = left,j = right;
while(i < j) {
while(j > i && a[j] > a[left]) j--;
while(i < j && a[i] <= a[left]) i++;
if(i < j) {
swap(a[i],a[j]);
}
}
swap(a[left],a[i]);
int dis = right - i + ;
if(dis == K){
return a[i];
}
else if(K < dis) {
return quickfind(a,i + ,right,K);
}
else{
return quickfind(a,left,i - ,K - dis);
}
}
};
网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array的更多相关文章
- 寻找第K大 网易2016实习研发工程师编程题
有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2] ...
- 【刷题-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 ...
- 网易 2016 实习研发project师 3道 编程题
1 比較重量 给定两颗钻石的编号g1,g2,编号从1開始.同一时候给定关系数组vector,当中元素为一些二元组.第一个元素为一次比較中较重的钻石的编号,第二个元素为较轻的钻石的编号.最后给定之前的比 ...
- WEB前端研发工程师编程能力成长之路(2)(转)
WEB前端研发工程师编程能力成长之路(2) 四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综 ...
- WEB前端研发工程师编程能力成长之路(1)(转)
WEB前端研发工程师编程能力成长之路(1) [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如果你是做了两三年WEB产品前端研发,迷茫找不着提高之路,看这篇文章吧: ...
- 寻找第K大的数
在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,解决这个问题的方法很多. 所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找 ...
- 快速排序 && 寻找第K大(小)的数
参考:https://minenet.me/2016/08/24/quickSort.html 快速排序 利用分治法可将快速排序的分为三步: 在数据集之中,选择一个元素作为"基准" ...
- [SOJ]寻找第k大数字(numberk)
Description 经过长时间的筹备工作,在Jourk,Ronny,Plipala,阿长,阿沈等人的努力下,DM实验室建立起自己的系列网站,其中包括三个大板块:DMOJ首页.DMOJ论坛.DMOJ ...
- 寻找第K大的数(快速排序的应用)
有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数.给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在.测试样例:[1,3,5,2,2],5, ...
随机推荐
- linux 使用wget下载https连接地址cannot verify github.com's certificate
使用linux的wget下载时候会出现网站没有证书警告的问题, 例如下载git时,可以使用wget https://github.com/git/git/archive/v2.3.0.zip --no ...
- cocos2dx 加密spine文件遇到的问题(暂时没有解决方法)
今天我研究了一下加密spine动画的加密的方法,图片肯定要加密的,所以我只选择加密图片,另外的一个altas文件和json文件就不做加密打算. 我的思路是通过TexturePacker打包成加密的文件 ...
- 洛谷 P1593 因子和
https://www.luogu.org/problemnew/show/P1593#sub 利用约数和定理:可以去看一下公式第13条 然后这个题目的话,要求$a^b$,那么我们首先可以先将a分解然 ...
- 二十一、C++中的临时对象
思考: 构造函数是一个特殊的函数 是否可以直接调用? 是否可以在构造函数中调用构造函数? 直接调用构造函数的行为是什么? 答: 直接调用构造函数将产生一个临时对象 临时对象的生命周期只有一条语句的时间 ...
- Spring框架配置文件中有两个相同名字的bean,最后会覆盖掉一个bean
问题容易出现在多个人合作的项目中,定义bean的名字的时候发生重复. 可以配置当bean定义重复的时候抛出异常,结束程序,强制提示更改重复的bean.
- C#基础-数组-ArrayList
数组ArrayList using System.Collections; //表示引入集合的命名空间 数组ArrayList容量本身是不固定的,根据存储的数据动态变化 // 声明一个ArrayLis ...
- navicat12.0.24破解方法,简单易操作,亲测可行
navicat12.0.24 32bit 链接:https://pan.baidu.com/s/1dakPje0AzwE86p6ZRHfnsQ 密码:f1ve 破解文件 链接:https://pan. ...
- R-codes-tips
1. 在shell执行R文件 chmod 0755 file.R Rscript file.R 2. 载入数据 data(dune) 3. attach() 将data.frame添加到R的搜索路径 ...
- JAVA基础篇—异常
五种常见异常 1.NullPointerException 空指针 2.ClassNotFoundException 指定类不存在 3.ArithmeticException运算异常 4.ArrayI ...
- HDU - 4763 Theme Section (KMP的next数组的应用)
给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...