414. 第三大的数

sorted不符合题意,但是也通过了 

 class Solution:
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        A = sorted(list(set(nums)))
        if len(A) < 3:
            return max(A)
        else:
            A.reverse()
            return A[2]

正解,思路很棒!

class Solution(object):
    def thirdMax(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        a = set(nums)
        if len(a) < 3:
            return max(a)
        else:
            a.remove(max(a))
            a.remove(max(a))
            return max(a)

 

相关文章:

  • 2021-09-08
  • 2021-07-25
  • 2022-12-23
  • 2021-08-13
  • 2021-05-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-05
  • 2022-12-23
  • 2021-12-20
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案