Intuitive Python solution


  • 0
    P

    Use set function to remove the duplicated number and check the length of the remain list to decide which number to return.

    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums = list(set(nums))
        nums.sort()
        if len(nums) < 3:
            return nums[-1]
        return nums[-3]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.