leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array
1 题目
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.
接口:int findKthLargest(int[] nums, int k)
2 思路
找出数组中第k大的元素。
分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。
3 代码
/**
* 偷懒的做法
*/
public int findKthLargest0(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
/**
* 分治的思想,利用partition的过程结果,第k的数,在排序后的len - k的位置。
*/
public int findKthLargest(int[] nums, int k) {
int len = nums.length;
int goal = len - k;
int left = 0, right = len - 1;
int index = partition(nums, left, right);
while (index != goal) {
if (index < goal) {
left = index + 1;
} else {
right = right - 1;
}
index = partition(nums, left, right);
}
return nums[goal];
}
private int partition(int[] a, int p, int r) {
int i = p - 1;
int x = a[r];
for (int j = p; j < r; j++) {
if (a[j] <= x) {
i++;
swap(a, i, j);
}
}
swap(a, i + 1, r);
return i + 1;
}
private void swap(int[] a, int i, int j) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
4 总结
分治
leetcode面试准备:Kth Largest Element in an Array的更多相关文章
- 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
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 【LeetCode】215. Kth Largest Element in an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:移除最大值 方法二:排序 方法三:大顶堆 方 ...
- LeetCode OJ 215. 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 so ...
- 【leetcode】215. 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 so ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- 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 ...
随机推荐
- JavaScript高级程序设计(十):数组类型
先导: 访问对象属性使用的都是点表示法.另外,我们还可以使用方括号表示法来访问对象的属性.在使用方括号的时候,应该将要访问的属性以字符串的形式放到方括号中.这两种方法没有什么区别.但是方括号的优点是可 ...
- artice与section的区别
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Objective-C 之同步请求、异步请求、GET请求、POST请求
本篇文章由:http://www.sollyu.com/objective-c-synchronization-requests-requests-for-asynchronous-requests- ...
- Beyond Compare 使用介绍
Beyond Compare 背景 平时工作中对于源代码都是使用SVN来管理,在线状态下工作的很好,但是有时候离线状态下,对于多个版本之间的代码合并就比较麻烦.尤其是涉及到多人协作时更是如此. 所以找 ...
- text与button上下不对齐解决方法
火狐可以对齐,但是IE8不行,加上浮动就可以了 .search_right input[type=button] { float:right; }
- linux下tomcat的安装
本文主要内容: (1)安装apr,这是 Apache 为了提升 Tomcat 的性能搞的一套本地化 Socket, Thread, IO 组件也就是说它有高级 IO 功能, 操作系统级别的功能调用, ...
- Linux procfs详解
1.0 proc文件系统总览在类Unix系统中体现了一种良好的抽象哲学,就是几乎所有的数据实体都被抽象成一个统一的接口--文件来看待,这样我们就可以用一些简单的基本工具完成大量复杂的操作.在Linux ...
- 如何把jar包放到本地maven仓库中
以前开发过程中也碰到过maven项目中需要引用其它jar包的情况,当时由于对maven还不熟悉,所以使用了在本地搭建nexus或者手动添加jar包的方法,两者都不是很方便. 如今因为淘宝的工作需要,又 ...
- platform_driver_register()--如何match之后调用probe
int platform_driver_register(struct platform_driver *drv) { drv->driver.bus = &platform_bus_t ...
- Linux统计某文件夹下文件、文件夹的个数
统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...