-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path414.linningmii.py
57 lines (48 loc) · 1.47 KB
/
414.linningmii.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution:
def thirdMax(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_num = nums[0]
second_num = None
third_num = None
for i in range(len(nums)):
if i == 0:
continue
# 对比 max_num 并尝试在 second_num 为空时为 second_num 赋值
if nums[i] > max_num:
third_num = second_num
second_num = max_num
max_num = nums[i]
continue
elif nums[i] == max_num:
continue
elif second_num is None:
second_num = nums[i]
continue
if second_num is None:
continue
# 对比 second_num 并尝试在 third_num 为空时为 third_num 赋值
if nums[i] > second_num:
third_num = second_num
second_num = nums[i]
continue
elif nums[i] == second_num:
continue
elif third_num is None:
third_num = nums[i]
continue
if third_num is None:
continue
if nums[i] > third_num:
third_num = nums[i]
continue
if third_num is not None:
return third_num
else:
return max_num
s = Solution()
print(s.thirdMax([1, 1, 2, 1]))
print(s.thirdMax([1, 2]))
print(s.thirdMax([1, 2, 2]))