215. Kth Largest Element in an Array(QuickSort)
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进行两侧的排序,再选一侧递归查找。
[一刷]:
- Kth函数中要调用一次qs函数. qs函数要写成private int, 所有变量都要重新声明类型。{调用qs},{qs}要分开写。
- divide的过程要一直做,所以都用while(i < j)的大循环包起来,一直在pivot两侧找,找到后交换一次即可
[二刷]:
- 忘记写corner case了。。
- 不是length,是nums.length。nums.length是数组长度,nums.length-1是最后一位
- 不用给声明的参数制定具体数,调用时才指定
[总结]:两根指针,联想两个小人。
[复杂度]:
快选,每次选一部分,扔掉另一部分,所以是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)的更多相关文章
- 【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 ...
- LeetCode Kth Largest Element in an Array (快速排序)
题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,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 ...
- 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 ...
- 【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 ...
- 【刷题-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 ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
随机推荐
- mysql 字符集排查
mysql 字符集排查 库级别 SELECT * FROM information_schema.schemata WHERE schema_name NOT IN ( 'information_sc ...
- python中获取当前路径并添加到系统路径
import os import sys sys.path.append(os.getcwd())
- sqoop操作之ORACLE导入到HIVE
导入表的所有字段 sqoop import --connect jdbc:oracle:thin:@192.168.1.107:1521:ORCL \ --username SCOTT --passw ...
- django框架ajax
参考 博文https://www.cnblogs.com/yuanchenqi/articles/9070966.html Ajax 简单示例: file_put文件上传页面: <!DOCTYP ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #19 ext4的调整
HACK #19 ext4的调整 本节介绍可以从用户空间执行的ext4调整.ext4在sysfs中有一些关于调整的特殊文件(见表3-6).使用这些特殊文件,就不用进行内核编译.重启,直接从用户空间确认 ...
- vim编程设置
在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进 等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc.在启动vim时,当前用户 ...
- linux开发模式
linux已被使用vim[文本编辑]+gcc[编译]+[gdb代码调试]开发模式 简单设置下开发环境,像设定vim的语法高亮,编辑c时代码自动缩进,tab缩进字符,显示行号等 编辑vinrc一般vin ...
- centos7.3nginx配置二级域名过程
nginx1.10.2 1先检查 /etc/nginx/nginx.conf 是否include conf.d include /etc/nginx/conf.d/*.conf; 默认都是包含的,如 ...
- HTML5 Canvas ( 填充图形的绘制 ) closePath, fillStyle, fill
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 1.Redis的应用场景
转自:http://www.runoob.com/redis/redis-tutorial.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在 ...