algorithm notes: leetcode#414 third maximum number

Problem


Analysis


Solution


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class :
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxNums = [float('-Inf')] * 3
for num in nums:
if num not in maxNums:
if num > maxNums[0]:
maxNums = [num, maxNums[0], maxNums[1]]
elif num > maxNums[1]:
maxNums = [maxNums[0], num, maxNums[1]]
elif num > maxNums[2]:
maxNums[2] = num
return maxNums[-1] if float('-Inf') not in maxNums else maxNums[0]

414. Third Maximum Number