My straightforward and self-explained Python solution


  • 0
    Z

    Maybe use three if and elif statement in the for loop will make program do slightly less work I guess.

    class Solution(object):
        def thirdMax(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            m1 = m2 = m3 = -sys.maxint
            for i in nums:
                m3, m2, m1 = (m2, m1, i) if i>m1 else (m3, m2, m1)
                m3, m2 = (m2, i) if m1>i>m2 else (m3, m2)
                m3 = i if m2>i>m3 else m3
            return m3 if not m3==-sys.maxint else m1
    

Log in to reply
 

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