414 Third Maximum Number
使用类似于215的解法,使用heapq包来进行解
import heapq
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums = [-i for i in nums]
heapq.heapify(nums)
maxmum = nums[0]
if len(nums) < 3:
return -heapq.heappop(nums)
for i in range(3):
ans = heapq.heappop(nums)
while len(nums) and nums[0] == ans:
heapq.heappop(nums)
if len(nums) == 0 and i != 2:
return -maxmum
return -ans