- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo414.java
39 lines (35 loc) · 863 Bytes
/
No414.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
38
39
packageAlgorithm.leetcode.Array;
importjava.util.HashSet;
/**
*
* Created by tujietg on Nov 1, 2019
*/
publicclassNo414 {
publicstaticintthirdMax(int[] nums) {
intmax = Integer.MIN_VALUE;
intsecond = Integer.MIN_VALUE;
intthree = Integer.MIN_VALUE;
HashSet<Integer> set = newHashSet<Integer>();
for (inti = 0; i < nums.length; i++) {
set.add(nums[i]);
if (nums[i] > max) {
three = second;
second = max;
max = nums[i];
} elseif (nums[i] > second && nums[i] < max) {
three = second;
second = nums[i];
} elseif ((nums[i] > three && nums[i] < second)) {
three = nums[i];
}
}
if (three == Integer.MIN_VALUE && set.size() < 3) {
returnmax;
} else {
returnthree;
}
}
publicstaticvoidmain(String[] args) {
System.out.println(thirdMax(newint[] { 5, 2, 4, 1, 3, 6, 0 }));
}
}