传送门

有一个整数数组,请你根据快速排序的思路,找出数组中第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

QuestionEditorial Solution

My Submissions

 
  • 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

Show Similar Problems
 
 
 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的更多相关文章

  1. 寻找第K大 网易2016实习研发工程师编程题

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,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 ...

  3. 网易 2016 实习研发project师 3道 编程题

    1 比較重量 给定两颗钻石的编号g1,g2,编号从1開始.同一时候给定关系数组vector,当中元素为一些二元组.第一个元素为一次比較中较重的钻石的编号,第二个元素为较轻的钻石的编号.最后给定之前的比 ...

  4. WEB前端研发工程师编程能力成长之路(2)(转)

    WEB前端研发工程师编程能力成长之路(2)   四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综 ...

  5. WEB前端研发工程师编程能力成长之路(1)(转)

    WEB前端研发工程师编程能力成长之路(1)   [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如果你是做了两三年WEB产品前端研发,迷茫找不着提高之路,看这篇文章吧: ...

  6. 寻找第K大的数

    在一堆数据中查找到第k个大的值. 名称是:设计一组N个数,确定其中第k个最大值,这是一个选择问题,解决这个问题的方法很多. 所谓“第(前)k大数问题”指的是在长度为n(n>=k)的乱序数组中S找 ...

  7. 快速排序 && 寻找第K大(小)的数

    参考:https://minenet.me/2016/08/24/quickSort.html 快速排序 利用分治法可将快速排序的分为三步: 在数据集之中,选择一个元素作为"基准" ...

  8. [SOJ]寻找第k大数字(numberk)

    Description 经过长时间的筹备工作,在Jourk,Ronny,Plipala,阿长,阿沈等人的努力下,DM实验室建立起自己的系列网站,其中包括三个大板块:DMOJ首页.DMOJ论坛.DMOJ ...

  9. 寻找第K大的数(快速排序的应用)

    有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数.给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在.测试样例:[1,3,5,2,2],5, ...

随机推荐

  1. Roman Numeral Converter-freecodecamp算法题目

    Roman Numeral Converter 1.要求 将给定的数字转换成罗马数字 所有返回的罗马数字都应该是大写形式 2.思路 分别定义个位.十位.百位.千位的对应罗马数字的数组 用Math.fl ...

  2. OpenCV3.42+VS2017配置+模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    目录 OpenCV3.42+VS2017配置 Visual Studio 2017 第三方依赖设置,附加依赖项和附加库目录 "fatal error LNK1112: 模块计算机类型&quo ...

  3. 【SAM】loj#6401. 字符串

    网上有篇题解写的是线段树合并维护求值? 题目描述 有一个只包含小写字母,长度为 $n$ 的字符串 $S$ .有一些字母是好的,剩下的是坏的. 定义一个子串 $S_{l\ldots r}$是好的,当且仅 ...

  4. sql_autoload_register()函数

    复习__autoload的时候,看到了spl_autoload_register()这个函数.但是一下子没有弄明白,通过查资料我算是弄明白了. 1.__autoload()    ——    自动加载 ...

  5. Docker 容器的数据管理

    docker 容器的数据卷 什么是数据卷(DataVolume) 数据卷是经过特殊计的目录,可以绕过联合文件系统(UFS),为一个或多个容器提供访问. 数据卷设计的目的,在于数据的永久化,它完全独立与 ...

  6. 阿里大鱼短信发送 FOR DT

    //增加了参数$action 来标志发送的是什么短信 注册短信 验证码短信 提示短信等 function send_sms($mobile, $message, $word = 0, $time = ...

  7. manjaro中virtualbox(vbox)配置

    一.更新源的配置: 1).自动方法: 在 终端 执行下面的arch" style="color: #002be5">命令从官方的源列表中对中国源进行测速和设置 su ...

  8. 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇

    import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...

  9. 一个通用的Makefile框架

    先做一个简单的记录,后续有时间再慢慢完善补充细节. 先上一个整体图片: 其中,最重要的文件就是:program_template.mk. 下面是program_template.mk最重要的内容: $ ...

  10. 运行Android程序出错:The connection to adb is down, and a severe error has occured

    调试Android程序时候,报错如下: [2013-02-21 15:41:06 - MainActivity] ------------------------------[2013-02-21 1 ...