Medium
https://leetcode.com/problems/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.
Example 1:
1 |
Input: [3,2,1,5,6,4] and k = 2 |
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length
2019.9.16 没做出来,参考之前写的 TopKth.py 写的,还是没有完全掌握啊唉
方法:
快排
一开始正确写出了 search(arr, l, r) 函数(也就是通常的 partition() 函数),但是写错了主函数里的循环。
1 |
class (object): |
类似题目:
Wiggle Sort II
Third Maximum Number
Kth Largest Element in a Stream
K Closest Points to Origin
近期评论