-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (33 loc) · 985 Bytes
/
Solution.java
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
class Solution {
public int thirdMax(int[] nums) {
Integer maxNum = null,
secondNum = null,
thirdNum = null;
for (int num : nums) {
if (maxNum == null) {
maxNum = num;
} else {
maxNum = Math.max(maxNum, num);
}
}
for (int num : nums) {
if (num == maxNum) {
continue;
} else if (secondNum == null) {
secondNum = num;
} else {
secondNum = Math.max(secondNum, num);
}
}
for (int num : nums) {
if (num == maxNum || num == secondNum) {
continue;
} else if (thirdNum == null) {
thirdNum = num;
} else {
thirdNum = Math.max(thirdNum, num);
}
}
return thirdNum != null ? thirdNum : maxNum;
}
}