LeetCode 414. [Python] Third Maximum Number
原题链接
简单
作者:
徐辰潇
,
2020-02-23 05:06:08
,
所有人可见
,
阅读 863
class Solution:
#Time Complexity: O(n)
#Space Complexity: O(1)
def thirdMax(self, nums: List[int]) -> int:
if (len(nums) < 3):
return max(nums)
max1 = max(nums)
max2 = -sys.maxsize
max3 = -sys.maxsize
for ele in nums:
if ele == max1:
continue
if max2 == -sys.maxsize:
max2 = ele
else:
if ele > max2:
max3 = max2
max2 = ele
elif ele < max2:
max3 = max(ele, max3)
if max3 == max2 or max3 == -sys.maxsize:
return max1
else:
return max3