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 ...
随机推荐
- Android Studio-设置快速转换局部变量为成员变量
"File"-"Settings"-"KeyMap"-"Main Menu"-"Refactor"- ...
- asp.net mvc 实现博客的时间分类管理
先看效果 这个其实用c#实现起来比较简单: Sides = bllSession.IArticleBLL.GetList("") .Select(a => a.Time) . ...
- 从svn检出项目---------不是web项目
javaweb项目从svn检出变成java项目 javaweb项目从svn检出后变成java项目,解决办法是: 1.项目右键–properties–Project Facets,勾选上Dynamic ...
- mysql sql语句执行时间查询
第一种:show profiles 之类的语句来查看 1.查一下profile是不是打开了,默认是不打开的. mysql> show profiles; Empty set (0.02 sec) ...
- Ionic 常见问题及解决方案
前言 Ionic是目前较为流行的Hybird App解决方案,在Ionic开发过程中会遇到很多常见的开发问题,本文尝试对这些问题给出解决方案. 一些常识与技巧 list 有延迟,可以在ion-cont ...
- mysql 总结二(自定义存储过程)
mysql执行流程: sql命令--->mysql引擎-----(分析)---->语法正确-----(编译)--->可识别命令----(执行)---->执行结果---(返回)- ...
- UML之用例图
用例图概要 ²用例图是被称为参与者的外部用户所能观察到的系统功能的模型图. (<UML参考手册>) ²用例图列出系统中的用例和系统外的参与者,并显示哪个参与者参与了哪个用例的执行 (或称为 ...
- 利用session防止用户未经登录而直接访问
在编写项目的时候,突然想如果按常理出牌,不首先进入登录界面而直接访问网页内容,可不可以呢?如此一来便尝试了一下,整的可以直接进入管理员页面,获取完全的管理权限.于是在网上查看了一下解决方案,学习了一下 ...
- 【codevs1380】没有上司的舞会
题目描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数 ...
- jquery选择器(一)-基础选择器
1. ID元素选择器 $("#btn1") 2. class元素选择器 $(".btn") 3. 标签元素选择器 $("div") 4. 全 ...