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. Linux查看关闭进程

    ps:进程的静态列表(Process status) - PID:进程号,每个进程独一无二的标识符(关闭进程需要使用) - TTY:终端所属,表明进程产生于哪一个终端,对于多用户使用的Linux服务器 ...

  2. 2018-8-10-win10-UWP-发邮件

    title author date CreateTime categories win10 UWP 发邮件 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 17 ...

  3. Spring STS 开发IDE下载安装

    https://spring.io/tools https://spring.io/tools3/sts/all/ Spring STS,全称是SpringSource Tool Suite ,是由s ...

  4. 条款7:为多态基类析构函数声明为virtual

    基类指针指向子类对象. 子类对象必须位于堆.因此为了避免泄漏内存资源,当指针不使用时,delete掉每一个对象非常重要.但是如果基类的析构函数不声明为virtual.那么指向子类对象的指针delete ...

  5. python常用函数 I

    iter(iterable) 可以生成一个迭代器. 例子: islice(iterator, int, int) itertools的islice方法为迭代器生成器提供切片操作. 例子: izip_l ...

  6. ssm科普篇

    springMVC执行步骤: 1.用户发送请求到前端控制器,前端控制器根据请求信息来决定选择页面控制器,并将请求委托给它 2.页面控制器收到请求后,进行功能处理,首先需要收集和绑定请求参数到一个对象, ...

  7. GSL+DevC++使用

    在DEV C++中配置GSL1.8库 前面写了如何在vs2005中添加gsl,本文所所述为在dev c++中使用gsl库,由实践总结而得. 准备软件: 1.Orwell Dev C++ 5.6.2 N ...

  8. 一张图理解"Figure", "Axes", "Axis"

    Figure is the object with the highest level in the hierarchy. It corresponds to the entire graphical ...

  9. 【leetcode】1021. Remove Outermost Parentheses

    题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...

  10. php nl2br()函数 语法

    php nl2br()函数 语法 作用:在字符串中的新行(\n)之前插入换行符dd马达 语法:nl2br(string,xhtml) 参数: 参数 描述 string 必须.规定要检查的字符串. xh ...