原题链接在这里:https://leetcode.com/problems/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.

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.

题解:

quickSelect 算法. 找第k大等于找,第num.length-k小.

findK就是找第k小函数, k从0开始.

递归终止条件是 start>=end, 或者start == end, 此时返回nums[start].

Time Complexity: O(n). Space O(1).

AC Java:

 class Solution {
public int findKthLargest(int[] nums, int k) {
if(nums == null || nums.length < k){
return -1;
} return findKth(nums, nums.length - k, 0 , nums.length - 1);
} private int findKth(int [] nums, int k, int l, int r){
if(l >= r){
return nums[l];
} int m = partition(nums, l, r);
if(m == k){
return nums[m];
}else if(m < k){
return findKth(nums, k, m + 1, r);
}else{
return findKth(nums, k, l, m - 1);
}
} private int partition(int [] nums, int l, int r){
int pivot = nums[l];
while(l < r){
while(l < r && nums[r] >= pivot){
r--;
} nums[l] = nums[r]; while(l < r && nums[l] <= pivot){
l++;
} nums[r] = nums[l];
} nums[l] = pivot;
return l;
} }

跟上Top K Frequent Elements, 找出Kth frequent element.

LeetCode Kth Largest Element in an Array的更多相关文章

  1. [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 ...

  2. 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 ...

  3. LeetCode Kth Largest Element in an Array (快速排序)

    题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...

  4. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  5. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  6. 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 ...

  7. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  8. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

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

  9. 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 ...

随机推荐

  1. linux 安装vbox增强工具

    首先在虚拟机控制台点设备--------安装增强功能,这样会用虚拟光驱加载增强功能镜象. 然后打开终端,先转到root身份:=================su================= f ...

  2. Leetcode Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  3. Linq to Entities下存储过程的使用方法

    1.首先在数据库中创建好存储过程. 2.在实体模型中添加存储过程的映射.此时根据映射过来的查询结果有两种途径:第一种可以选择添加选择的存储过程的函数到实体模型中.这样的话,查询的结果将会是xxx_re ...

  4. tableFooterView中的按钮点击没反应

    一,经历 1.查了按钮没有响应的几个方法,排除了是用户交互设置为 NO 的情况. 2.然后打印了一下tableFooterView,尽然发现其高度为0,而且我也没有设置 frame, 却可以显示按钮, ...

  5. Java_动态加载类(英文)

    It is possible to load and reload classes at runtime in Java, though it is not as straightforward as ...

  6. Windows 下安装使用docker swarm machine docker toolbox

    下载docker 集成安装环境 http://get.daocloud.io/#install-toolbox 这个网站很不错,下载 这个集成了 docker docker-machine ,还有gi ...

  7. 在Eclipse中在线安装Emmet和图文使用教程

    ZenCoding 升级为 Emmet 之后,基于 Eclipse 的插件安装地址也发生了变化, 下面是在基于 Eclipse 的 IDE 中安装和使用 Emmet 的图文示例. 一.打开 Eclip ...

  8. Color Processing 色彩处理

    How to read and write pixel data Bitmap processing Image Stride Bayer Color Filter Pattern High-Qual ...

  9. java设计模式。。。转载

    maowang I am a slow walker,but I never walk backwards! 博客园 首页 新随笔 联系 订阅 管理 随笔 - 125  文章 - 0  评论 - 12 ...

  10. .net 的 Url 中文加密

    当 Get 的请求参数可能出现中文的时候,那么就需要对其中文进行加密处理: 引用程序集:System.Web 加密方法: HttpUtility.UrlEncode(); 解密方法:HttpUtili ...