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 ...
随机推荐
- motto2
Baby you've done enough that cut your breath.Don't beat yourself up don't need to turn so fast.Somet ...
- vim does not map customized key?
it is a long time confusing me that why my customized key map in vim does not work? Some vim configu ...
- php基础语法-函数等
php是弱类型语言, 但并不是 无类型 语言! 同样有变量类型, ====================================================== 形容词(短语)修饰名词的 ...
- PHP 使用命名空间(namespace),实现自动加载
示例: #/DB/MySql.class.php也就是DB文件夹下有MySql.class.php文件 namespace DB; class MySql { public function __co ...
- iOS: ARC & MRC下string内存管理策略探究
ARC & MRC下string内存管理策略探究 前两天跟同事争论一个关于NSString执行copy操作以后是否会发生变化,两个人整了半天,最后写代码验证了一下,发现原来NSString操作 ...
- WCF :IIS寄宿方式的Web地址、BaseAddress和EndPoint Address的关系
对于在IIS中通过W3SVC或WAS寄宿的WCF Service,其在浏览器中显示的地址(Web地址),与其配置文件中的BaseAddress和EndPoint Address有什么关系呢?让我们来分 ...
- CSS 补充
属性选择器下面的例子为带有 title 属性的所有元素设置样式:[title]{ color:red;} <h1>可以应用样式:</h1><h2 title=" ...
- tc 146 2 BridgeCrossing(n人过桥问题)
SRM 146 2 1000BridgeCrossing Problem Statement A well-known riddle goes like this: Four people are c ...
- 【PHP面向对象(OOP)编程入门教程】13.访问类型(public,protected,private)
类型的访问修饰符允许开发人员对类成员的访问进行限制,这是PHP5的新特性,但却是OOP语言的一个好的特性.而且大多数OOP语言都已支持此特性.PHP5支持如下3种访问修饰符: public (公有的. ...
- python下载网页源码 写入文本
import urllib.request,io,os,sysreq=urllib.request.Request("http://echophp.sinaapp.com/uncategor ...