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大的数字,怎么求呢,当然首先想到的是给数组排序,然后求可以得到第k大的数字。先看一种利用 C++ 的 STL 中的集成的排序方法,不用我们自己实现,这样的话这道题只要两行就完事了,代码如下:

解法一:

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
return nums[nums.size() - k];
}
};

下面这种解法是利用了 priority_queue 的自动排序的特性,跟上面的解法思路上没有什么区别,当然我们也可以换成 multiset 来做,一个道理,参见代码如下:

解法二:

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> q(nums.begin(), nums.end());
for (int i = ; i < k - ; ++i) {
q.pop();
}
return q.top();
}
};

上面两种方法虽然简洁,但是确不是本题真正想考察的东西,可以说有一定的偷懒嫌疑。这道题最好的解法应该是下面这种做法,用到了快速排序 Quick Sort 的思想,这里排序的方向是从大往小排。对快排不熟悉的童鞋们随意上网搜些帖子看下吧,多如牛毛啊,总有一款适合你。核心思想是每次都要先找一个中枢点 Pivot,然后遍历其他所有的数字,像这道题从大往小排的话,就把大于中枢点的数字放到左半边,把小于中枢点的放在右半边,这样中枢点是整个数组中第几大的数字就确定了,虽然左右两部分各自不一定是完全有序的,但是并不影响本题要求的结果,因为左半部分的所有值都大于右半部分的任意值,所以我们求出中枢点的位置,如果正好是 k-1,那么直接返回该位置上的数字;如果大于 k-1,说明要求的数字在左半部分,更新右边界,再求新的中枢点位置;反之则更新右半部分,求中枢点的位置;不得不说,这个思路真的是巧妙啊~

解法三:

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int left = , right = nums.size() - ;
while (true) {
int pos = partition(nums, left, right);
if (pos == k - ) return nums[pos];
if (pos > k - ) right = pos - ;
else left = pos + ;
}
}
int partition(vector<int>& nums, int left, int right) {
int pivot = nums[left], l = left + , r = right;
while (l <= r) {
if (nums[l] < pivot && nums[r] > pivot) {
swap(nums[l++], nums[r--]);
}
if (nums[l] >= pivot) ++l;
if (nums[r] <= pivot) --r;
}
swap(nums[left], nums[r]);
return r;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/215

类似题目:

Wiggle Sort II

Top K Frequent Elements

Third Maximum Number

Kth Largest Element in a Stream

K Closest Points to Origin

参考资料:

https://leetcode.com/problems/kth-largest-element-in-an-array/

https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained

https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60309/C%2B%2B-PartitionMax-Heappriority_queuemultiset

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字的更多相关文章

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

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

  3. 215 Kth Largest Element in an Array 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假 ...

  4. 前端算法题:找出数组中第k大的数字出现多少次

    题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...

  5. LeetCode Kth Largest Element in an Array (快速排序)

    题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...

  6. [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  7. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  8. LeetCode——Kth Largest Element in an Array

    Description: Find the kth largest element in an unsorted array. Note that it is the kth largest elem ...

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

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

随机推荐

  1. Windows环境下vscode-go安装笔记

    一.介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github.com/microsoft/vscode-go 这款插件的特性包括: ...

  2. 简简单单学会C#位运算

    一.理解位运算 要学会位运算,首先要清楚什么是位运算?程序中的所有内容在计算机内存中都是以二进制的形式储存的(即:0或1),位运算就是直接对在内存中的二进制数的每位进行运算操作 二.理解数字进制 上面 ...

  3. MyBatis通过JDBC生成的执行语句问题

    我们编程的过程中大部分使用了很出色的ORM框架,例如:MyBatis,Hibernate,SpringJDBC,但是这些都离不开数据驱动JDBC的支持.虽然使用起来很方便,但是碰到一些问题确实很棘手, ...

  4. Boost条件变量condition_variable_any

    Boost条件变量可以用来实现线程同步,它必须与互斥量配合使用.使用条件变量实现生产者消费者的简单例子如下,需要注意的是cond_put.wait(lock)是在等待条件满足.如果条件不满足,则释放锁 ...

  5. 口碑外卖系统架构图(li)

  6. Spring+ibatis动态管理数据源

    Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时 ...

  7. CSS代码规范

    空格 选择器 与 { 之间必须包含空格. 列表型属性值 书写在单行时,, 后必须跟一个空格. 属性名 与之后的 : 之间不允许包含空格, : 与 属性值 之间必须包含空格. margin: 0; .+ ...

  8. SharePoint如何将使列表不被爬网爬到。

    有一个项目,没有对表单进行严格的权限管理,虽然用户在自己的首页只能看到属于的单子,但是在搜索的时候,所有人的单子都能被搜到,所以客户造成了困惑. 那么问题来了,怎么让列表或者文档库不被爬网爬到. 有两 ...

  9. KOTLIN开发语言文档(官方文档) -- 入门

    网页链接:https://kotlinlang.org/docs/reference/basic-syntax.html 1.   入门 1.1.  基本语法 1.1.1.   定义包 包说明应该在源 ...

  10. Android Weekly Notes Issue #230

    Android Weekly Notes Issue #230 November 6th, 2016 Android Weekly Issue #230. Android Weekly笔记, 本期内容 ...