215 Kth Largest Element in an Array
# 215 Kth Largest Element in an Array
题目来源:
https://leetcode.com/problems/kth-largest-element-in-an-array/description/
题意分析:
在一个无序链表中找出第k大的元素。
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
题目思路:
第一反应就是可以用数据结构--堆,直接查了库heapq就可以用了,discuss中也有用优先队列的,当然其实优先队列的底层实现也是堆。
代码:
1. class Solution:
2. def findKthLargest(self, nums, k):
3. """
4. :type nums: List[int]
5. :type k: int
6. :rtype: int
7. """
8. import heapq
9. # heapq.heapify(nums)
10. res=heapq.nlargest(k,nums)
11. return res[-1]
12.
提交细节: