forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirdMaximumNumber.java
More file actions
35 lines (30 loc) · 857 Bytes
/
ThirdMaximumNumber.java
File metadata and controls
35 lines (30 loc) · 857 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
33
34
35
/**
* 这题注意边界情况
*/
public class ThirdMaximumNumber {
public int thirdMax(int[] nums) {
int count = 0;
long first = (long) Integer.MIN_VALUE - 1;
long second = (long) Integer.MIN_VALUE - 1;
long third = (long) Integer.MIN_VALUE - 1;
for (int n : nums) {
if (n == first || n == second || n == third) {
continue;
}
if (n > first) {
count++;
third = second;
second = first;
first = n;
} else if (n > second) {
count++;
third = second;
second = n;
} else if (n >= third) {
count++;
third = n;
}
}
return (int) (count >= 3 ? third : first);
}
}