49.Kth Largest Element in an Array
Level:
Medium
题目描述:
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.
Example 1:
Input: [3,2,1,5,6,4] and k = 2
Output: 5
Example 2:
Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
思路分析:
题目要求找出未排序数组的第K个最大的数,我们可以转换为找从小到大排列的第n-k个数。我们使用快速排序的partition函数,partition函数选择一个flag值,将数组中小于flag的值放在flag左边,将大于flag的值放在右边,我们要找第n-k个数,可以通过判断flag的位置确定,如果flag的位置正好是n-k,那我们找到答案,否则我们可以将范围缩小继续查找。
代码:
public class Solution{
public int findKthLargest(int[] nums,int k){
int n=nums.length-k;
int low=0;
int high=nums.length-1;
int t;
while(low<high){
t=partition(nums,low,high);
if(t>n){
high=t-1;
}else if(t<n){
low=t+1;
}else{
break;
}
}
return nums[n];
}
public int partition(int []nums,int low,int high){
int key=nums[low];
while(low<high){
while(low<high&&nums[high]>=key){
high--;
}
nums[low]=nums[high];
while(low<high&&nums[low]<=key){
low++;
}
nums[high]=nums[low];
}
nums[low]=key;
return low;
}
}
49.Kth Largest Element in an Array的更多相关文章
- Kth Largest Element in an Array
Find K-th largest element in an array. Notice You can swap elements in the array Example In array [9 ...
- leetcode面试准备:Kth Largest Element in an Array
leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...
- 【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 ...
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 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 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
- Lettcode Kth Largest Element in an Array
Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...
- 网易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 ...
随机推荐
- Solr的学习使用之(二)schema.xml等配置文件的解析
上一篇文章已经讲解了如何部署Solr,部署是部署完了,可是总觉得心里空空的,没底,里面有N多配置文件,比如schema.xml.solrConfig.xml.solr.xml and so on……都 ...
- 微信小程序(6)--获取屏幕宽度及弹窗滚动与页面滚动冲突
1.获取屏幕宽度,并赋值给view <view class="ships-img" style="height:{{windowWidth}}px;"&g ...
- Go 查找
sort.SearchInts(a []int, b int) 从数组a中查找b,前提是a必须有序 sort.SearchFloats(a []float64, b float64) 从数组a中查找b ...
- 通过注释查找mysql的表名
通过注释查找mysql的表名 select * from INFORMATION_SCHEMA.columns where COLUMN_NAME Like '%placement%';
- exists 的简单介绍
准备数据: CREATE TABLE Books( BookID number, BookTitle VARCHAR2(20) NOT NULL, Copyright varchar2(20) ) I ...
- sql 2008常用语法语句收集
EXEC sp_dropuser 'test' : 从当前数据库删除用户 test EXEC sp_droplogin 'test' : 从 SQL Server 中删除登录 test select ...
- springboot下的多数据源切换
今天在考虑如果分公司也用系统了,怎么办,是单独的数据库,还是一起使用?所以就想到了切换数据源来实现,但是发现,只是读写分离,还要再改一下,根据用户地域来切换数据源,今天先照着例子做一下. 看了好多文章 ...
- Navicat for Mysql 连接报错1251[已解决]
因为mysql是8.0.11的版本,比较新的mysql采用新的保密方式所以旧的修改密码的方式似乎不能用, 新版本改密码方式: use mysql; ALTER USER 'root'@'localho ...
- Linux:主设备号和次设备号
http://www.linuxidc.com/Linux/2011-03/33863.htm Linux的设备管理是和文件系统紧密结合的,各种设备都以文件的形式存放在/dev目录下,称为设备 ...
- (65)C# 任务
1.启动任务 //Framework4.5新增的Task.Run开启一个任务,Run方法中传入一个Action委托 Task.Run(()=> { Thread.Sleep(); Console ...