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.

[一句话思路]:快速选择:先利用pivot进行两侧的排序,再选一侧递归查找。

[一刷]:

  1. Kth函数中要调用一次qs函数. qs函数要写成private int, 所有变量都要重新声明类型。{调用qs},{qs}要分开写。
  2. divide的过程要一直做,所以都用while(i < j)的大循环包起来,一直在pivot两侧找,找到后交换一次即可

[二刷]:

  1. 忘记写corner case了。。
  2. 不是length,是nums.length。nums.length是数组长度,nums.length-1是最后一位
  3. 不用给声明的参数制定具体数,调用时才指定

[总结]:两根指针,联想两个小人。

[复杂度]:

快选,每次选一部分,扔掉另一部分,所以是O(N)
假设每次扔掉一半.
(2^k=N)
T(N) =n +n/2+n/4+n/8+n/2^k = n*(1-2^-k)/(1-2^-1) =2N

[英文数据结构]:array

[其他解法]:

[题目变变变]:median, Kth in two array's combination

class Solution {
public int findKthLargest(int[] nums, int k) {
return quickSelect(nums, 0, nums.length - 1, k);} private int quickSelect(int[] nums, int start, int end, int k) {
int i = start;
int j = end;
int pivot = nums[(start + end) / 2]; if (start == end) {
return nums[start];
} //从小到大排序
while (i <= j) {
while (i <= j && nums[i] > pivot) {
i++;
}
while (i <= j && nums[j] < pivot) {
j--;
} if (i <= j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp; i++;
j--;
}
} if (start + k - 1 <= j) {
return quickSelect(nums, start, j, k);
}
else if (start + k - 1 >= i) {
return quickSelect(nums, i, end, k - (i - start));
}
else
return nums[j + 1];
}
}

215. Kth Largest Element in an Array(QuickSort)的更多相关文章

  1. 【leetcode】Kth Largest Element in an Array (middle)☆

    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 (快速排序)

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

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

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

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

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

  5. LN : leetcode 215 Kth Largest Element in an Array

    lc 215 Kth Largest Element in an Array 215 Kth Largest Element in an Array Find the kth largest elem ...

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

  7. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

  8. 【刷题-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 ...

  9. 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...

随机推荐

  1. Python中的logger和handler到底是个什么鬼

    最近的任务经常涉及到日志的记录,特意去又学了一遍logging的记录方法.跟java一样,python的日志记录也是比较繁琐的一件事,在写一条记录之前,要写好多东西.典型的日志记录的步骤是这样的: 创 ...

  2. final修饰的类有什么特点

    变量定义为final,一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引用不可再变. 方法定义为final,是为了防止任何继承类改变它. 类定义为final, ...

  3. java内存模型(一)正确使用 Volatile 变量

    文章转载自: 正确使用 Volatile 变量   Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronize ...

  4. 杂: PYTHON上数据储存:推荐h5py

    一篇很短的小短文,主要推荐下做科学计算是大量数据的储存问题 最近在做一个CNN的项目,文件夹里有20w张图片要读入并保存到一个data文件(不然每次都读20w文件太麻烦). 折腾了一个下午,发现了一个 ...

  5. 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release &lt; 7.5-0.11" ?

    https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...

  6. mock——test 基本所有使用

    可以参考:http://www.cnblogs.com/lyy-2016/p/6122144.html test /** * */ package com.imooc.web.controller; ...

  7. leetcode171

    public class Solution { private int ConvertToC(char c) { ; switch (c) { case 'A': case 'a': rnt = ; ...

  8. sitemap

    sitemap对于网站就像是字典的索引目录,而这个目录的读者则是搜索引擎的爬虫.网站有了sitemap,有助于搜索引擎“了解”网站,这样会有助于站点的内容被收录. sitemap是一个由google主 ...

  9. Gson 解析教程

    Gson 是google解析Json的一个开源框架,同类的框架fastJson,JackJson等等 本人fastJson用了两年,也是从去年才开始接触Gson,希望下面的总结会对博友有用,至于Gso ...

  10. Activity工作流学习(一)——Activity服务类

    Activity有9个service1.DynamicBpmnService动态Bpmn服务Service providing access to the repository of process ...