a short python solution

  • 0
    class Solution(object):
        def thirdMax(self, a):
            x, y, z = a[0], float('-inf'), float('-inf')
            for e in a:
                if e > x: x, y, z = e, x, y
                elif y < e < x: y, z = e, y
                elif z < e < y: z = e
            return z if x != y != z and z != float('-inf') else x

Log in to reply

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