Java for 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 sorted order, not the kth distinct element.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
解题思路:
本题是《算法导论》原题,9.2节用归并的思路给出了时间复杂度为O(n)的算法,9.3节给出了最坏时间为O(n)的算法(就是讨论区的第一个),实现起来比较麻烦(笔者提交Wrong Answer了几次,不想搞了),本题最简单的快排即可实现,JAVA实现如下:
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
另外,网上看人用归并排序,传送如下:
public int findKthLargest(int[] nums, int k) {
return findK(nums, nums.length-k, 0, nums.length-1);
}
private int findK(int[] nums, int k, int i, int j) {
if(i>=j) return nums[i];
int m = partition(nums, i, j);
if(m==k) return nums[m];
else if(m<k) {
return findK(nums, k, m+1, j);
} else {
return findK(nums, k, i, m-1);
}
}
private int partition(int[] nums, int i, int j) {
int x = nums[i];
int m = i;
int n = i+1;
while(n<=j){
if(nums[n]<x) {
swap(nums, ++m, n);
}
++n;
}
swap(nums,i, m);
return m;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
Java for LeetCode 215 Kth Largest Element in an Array的更多相关文章
- 剑指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] 215. 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 ...
- 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 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- C#版 - Leetcode 215. Kth Largest Element in an Array-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 ...
随机推荐
- div+css使多行文字垂直居中?
1.单行文字: 设置height = line-height; 多行文字: 设置 padding, 自己要计算一下? vertical-align: 可以设置垂直居中, 但是只是针对本身就具有 ali ...
- python 运行时报错误SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2
File "1.py", line 2SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 2, but no ...
- 微信要革"传统电视"的命吗?
除夕夜不知大家是否发现微信摇一摇界面下方的菜单变成4个了?“红包,人,歌曲,电视”,红包和电视是新增的,几天之后红包这个菜单消失了,电视菜单还在,能够摇出一些电视台的直播节目单,以往的摇电视借用的是摇 ...
- 【C语言入门教程】7.1 结构体类型变量的定义和引用
前面学习了变量和数组这些简单的数据结构,它们的特点是必须使用规定的数据类型.例如数组被定义为整型后,它的所有存储单元都是由整型构成.现实生活中某一类事物的共同属性可能是由不同的数据类型组成的集合,或者 ...
- 浮动层-JS兼容IE6
//引用jquery 包/*orderBycat 与 orderBycatHead 双层浮动*/ $(window).scroll(function () { var top = $(window). ...
- Android 软键盘盖住输入框的问题
当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...
- 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理
导读 Nginx 是最快和最强大的 Web 服务器之一,以其高性能和低资源占用率而闻名.它既可以被安装为一个独立的 Web 服务器,也可以安装成反向代理 Web 服务器.在这篇文章,我将讨论在安装了 ...
- 快速入门SaltStack
导读 SaltStack是基于Python开发的一套C/S架构配置管理工具(功能不仅仅是配置管理,如使用salt-cloud配置AWS EC2实例),它的底层使用ZeroMQ消息队列pub/sub方式 ...
- Unity内存申请和释放
转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shad ...
- cocos2d内存管理
设想如下场景, 这是一个典型的内存合理分配的场景: 在一帧内, 有若干个函数, 每个函数都会创建一系列的精灵, 每个精灵都不同, 都会占用一定的内存, 精灵的总数可能会有1000个, 而一个函数只会创 ...