Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
求一列数第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]

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理