215 Kth Largest Element in an Array

使用heapq, 注意这个是小根堆, 每次出来的是最小的元素, 所以要两遍取负号~

import heapq
class Solution(object):
    def findKthLargest(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        nums = [-i for i in nums]
        heapq.heapify(nums)

        for i in range(k):
            ans = heapq.heappop(nums)
        return -ans

results matching ""

    No results matching ""