-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0414_third-maximum-number.py
More file actions
32 lines (24 loc) · 942 Bytes
/
0414_third-maximum-number.py
File metadata and controls
32 lines (24 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution:
def thirdMax(self, nums: List[int]) -> int:
first_max = max(nums)
nums = [num for num in nums if num != first_max]
if nums != []:
second_max = max(nums)
nums = [num for num in nums if num != second_max]
third_max = 0
if nums != []:
third_max = max(nums)
return third_max
else:
return first_max
# first, second, third = float(-inf), float(-inf), float(-inf)
# for num in nums:
# if num in (first, second, third):
# continue
# if num > first:
# first, second, third = num, first, second
# elif num > second:
# second, third = num, third
# elif num > third:
# third = num
# return third if third != float(-inf) else first