首页 > 代码库 > 414. Third Maximum Number

414. Third Maximum Number

https://leetcode.com/problems/third-maximum-number/#/description

 

class Solution(object):
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        
        # if len(nums) == 0:
        #    return None
        # elif len(nums) < 3:
        #    return max(nums)
        #else:
        v = [float(-inf), float(-inf), float(-inf)]
        for num in nums:
            if num not in v:
                if num > v[0]:
                    v = [num, v[0],v[1]]
                elif num > v[1]:
                    v = [v[0], num, v[1]]
                elif num > v[2]:
                    v = [v[0], v[1], num]
        if float(-inf) in v:
            return max(nums)
        return v[2]

 

414. Third Maximum Number