Sunday, December 3, 2017

414. Third Maximum Number

class Solution:
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        new_list = list(set(nums))
        sorted_new_list = sorted(new_list)
        try:
            return sorted_new_list[-3]
        except:
            return sorted_new_list[-1]

class Solution:
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a = b = c = float("-inf")
        for n in nums:
            if n > a:
                a, b, c = n, a, b
            elif a > n > b:
                b, c =n, b
            elif b > n > c:
                c = n
        return c if c != float("-inf") else a

No comments:

Post a Comment