首页 > 其他 > 详细

Kth Largest Element in an Array

时间:2016-04-24 12:47:25      阅读:177      评论:0      收藏:0      [点我收藏+]

利用最小堆解决,代码如下:

class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        heap=nums[0:k]
        heapq.heapify(heap)
        l=len(nums)
        for i in xrange(k,l):
           heapq.heappushpop(heap,nums[i])
        return heap[0]

 

Kth Largest Element in an Array

原文:http://www.cnblogs.com/sherylwang/p/5426669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!