原题链接在这里: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. unity meshrender理解

    网格渲染器,其中unity里面多有的材质在渲染的时候都是会划分成三角形的,所以当添加一些物体的时候,例如3d text的时候,默认添加网格渲染器. 最常用的就是获取材质. 下面是一个利用网格渲染器获得 ...

  2. ccc animation

    cc.Class({ extends: cc.Component, properties: { sheepAnim: { default: null, type: cc.Animation } }, ...

  3. 网易大手笔领投美国VR触觉公司AxonVR

    12月8日消息,美国西雅图VR触觉公司AxonVR今日宣布完成了580万美元的种子轮融资,由中国网易和Dawn Patrol Ventures领投.此次融资完成后AxonVR的总融资额将攀升至700万 ...

  4. Colorado Potato Beetle(CF的某道) & 鬼畜宽搜

    题意: 一个人在一张大图上走,给你路径与起点,求他走出的矩形面积并.(大概这个意思自行百度标题... SOL: 与其说这是一道图论题不如说是一道生动活泼的STL-vector教学.... 离散化宽搜, ...

  5. 用css3实现一个带缺口的圆圈(图)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. HighchartsJS创建点状带标识的图表实例

    上一篇我发布的是关于 HighchartsJS创建环形带标识的图表实例, 从那篇文章就可以看出 HighchartsJS 确实是一款功能很强大的图表库.利用它,我们可以在项目中创建出我们所需要的图表来 ...

  7. GUID相关知识。。。。转载

              全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络 ...

  8. 拿到添加对象的id号方法

    以前Hibernate添加对象,想拿到id号的时候都是根据id排序拿到第一条 ,才知道 这样也可以 /**         * @Description: 添加一个角色信息        * @ret ...

  9. Js的引用关系示例和总结

    三种引用(指针引用)关系,借助引用关系可以形成复杂的链关系,巧妙借助链关系可以实现收放自如,形散神不散的神奇效果,jquery就是其中一例: 1.对象指向属性;        2.a=b(b是对象,a ...

  10. spring security 管理会话 多个用户不可以使用同一个账号登录系统

    多个用户不能使用同一个账号同时登陆系统. 1. 添加监听器 在web.xml中添加一个监听器,这个监听器会在session创建和销毁的时候通知Spring Security. <listener ...