Python O(n)


  • 0
    class Solution(object):
        def thirdMax(self, nums):
            ans = []
            for x in nums:
                if x in ans:
                    continue
                ans.append(x)
                ans.sort(reverse=True)
                if len(ans) > 3:
                    ans.pop()
            return ans[-1] if len(ans) == 3 else ans[0]

Log in to reply
 

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