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
27 lines (25 loc) · 785 Bytes
/
ThirdMaximumNumber.java
File metadata and controls
27 lines (25 loc) · 785 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
/**
* 这题注意边界情况
*/
public class ThirdMaximumNumber {
public int thirdMax(int[] nums) {
long first = (long) Integer.MIN_VALUE - 1;
long second = first, third = first;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == first || nums[i] == second || nums[i] == third) {
continue;
}
if (nums[i] > first) {
third = second;
second = first;
first = nums[i];
} else if (nums[i] > second) {
third = second;
second = nums[i];
} else if (nums[i] > third) {
third = nums[i];
}
}
return (int) (third >= Integer.MIN_VALUE ? third : first);
}
}