呓语Beta 2.0 首页 镜头后 灶台前 捐赠者名单 有趣的小站

leetcode 414 Third Maximum Number

文档信息

leetcode 414 Third Maximum Number

解法一

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

note

捐赠支持

可以使用支付宝转帐给richard.ma.19850509#gmail.com(请将#替换为@),谢谢!

Fork me on GitHub