这一题很神奇,总觉得很简单, 但实际上在做的时候老出问题
class Solution(object):
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
v = [float("-inf"), float("-inf"), float("-inf")]
for num in nums:
if num not in v:
if num > v[0]:
v = [num, v[0], v[1]]
elif num > v[1]:
v = [v[0], num, v[1]]
elif num > v[2]:
v = [v[0], v[1], num]
if float("-inf") in v:
return v[0]
else:
return v[2]
···