求一列数第k大的数,heap应用
1 #215
2 #Runtime: 822 ms (Beats 46.88%)
3 #Memory: 23.3 MB (Beats 81.50%)
4
5 class Solution(object):
6 def findKthLargest(self, nums, k):
7 """
8 :type nums: List[int]
9 :type k: int
10 :rtype: int
11 """
12 hp = []
13 for n in nums:
14 heapq.heappush(hp, n)
15 if len(hp) > k:
16 heapq.heappop(hp)
17 return hp[0]