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的更多相关文章

  1. 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 ...

  2. leetcode面试准备:Kth Largest Element in an Array

    leetcode面试准备:Kth Largest Element in an Array 1 题目 Find the kth largest element in an unsorted array. ...

  3. 【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 ...

  4. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

  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 ...

  6. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

  7. Lettcode Kth Largest Element in an Array

    Lettcode Kth Largest Element in an Array 题意:在无序数组中,寻找第k大的数字,注意这里考虑是重复的. 一直只会简单的O(nlogn)的做法,听说这题有O(n) ...

  8. 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  9. 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 ...

随机推荐

  1. GeneXus笔记本—常用函数(下)

    这篇是常用函数的最后一节 当然 我这里聊的还不是全部的,需要各位朋友继续在工作中去深入才行啊 ,毕竟从入门到入土....┌(; ̄◇ ̄)┘ 1:Sleep 这个函数你们应该能猜到 ”To allow m ...

  2. python常用函数 M

    max(iterable) 求最大值,可以传入key. 例子: min(iterable) 求最小值,支持传入key. 例子: match(regular expression, string) 字符 ...

  3. 逗号导致hive报“SemanticException Error in parsing”错误

    > select p.dt, p.cookie_qunar_global, p.refer_domain, p.kwid, p.query_word, p,traffic_type--, p.p ...

  4. USACO2008 Jan 电话网络

    Time Limit: 10 Sec Memory Limit: 162 MB Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶 ...

  5. Quartz.Net 任务调度之时间策略(4)

    在复杂的业务逻辑中,cron可以灵活的制定时间策略 先说使用方法 ITrigger trigger = TriggerBuilder.Create() .WithIdentity("trig ...

  6. 【leetcode】623. Add One Row to Tree

    题目如下: Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with ...

  7. cf1278D——树的性质+并查集+线段树/DFS判环

    昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...

  8. 如何保留小数点后N位?

    2014年10月17日09:48:39 在做项目中遇到的,要把想显示的数据进行规定小数位的保留,下面写一下用过的方法: 1. BigDecimal 方法(我做项目的时候用的方法) 代码: java.m ...

  9. SelfCert wcf中 生成x5.09证书的工具

    http://blog.pluralsight.com/selfcert-create-a-self-signed-certificate-interactively-gui-or-programma ...

  10. 转载:Linux下启动和关闭Weblogic(管理服务器+被管服务器)

    转载自:http://www.cnblogs.com/nick-huang/p/3834134.html  感谢! Weblogic的管理服务器和被管服务器的启动.关闭,偶尔会用到,却又不常用,导致需 ...