-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path414.third-maximum-number.java
More file actions
43 lines (39 loc) · 1.49 KB
/
Copy path414.third-maximum-number.java
File metadata and controls
43 lines (39 loc) · 1.49 KB
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
class Solution {
public int thirdMax(int[] nums) {
if(nums.length == 0)
return 0;
if(nums.length <=2)
return nums.length == 1 ? nums[0] : Math.max(nums[0], nums[1]);
if(nums.length == 3){
return nums[0] == nums[1] ? Math.max(nums[2],nums[1]) : nums[2] == nums[1] ? Math.max(nums[0],nums[1]) : nums[0] == nums[2] ? Math.max(nums[2],nums[1]) :Math.min(Math.min(nums[0],nums[1]),nums[2]);
}
int a[] = new int[3];
a[0] = Integer.MIN_VALUE;
a[1] = Integer.MIN_VALUE;
a[2] = Integer.MIN_VALUE;
int t = 0;
// System.out.println(f+" "+s+" "+t);
for(int i=0;i<nums.length;i++){
if(nums[i] > a[t]){
if(nums[i] > a[t+2])
a[t+2] = a[t+1];
if(nums[i] > a[t+1])
a[t+1] = a[t];
a[t] = nums[i];
}
else if(nums[i] == a[t] || nums[i] == a[t+1]){
continue;
}
else {
if(nums[i] > a[t+1]){
a[t+2] = a[t+1];
a[t+1] = nums[i];
}
else if(nums[i] > a[t+2])
a[t+2] = nums[i];
}
}
//System.out.println(a[0]+" "+a[1]+" "+a[2]);
return Math.min(Math.min(a[0],a[1]),a[2]) == Integer.MIN_VALUE ? Math.max(Math.max(a[0],a[1]),a[2]) : Math.min(Math.min(a[0],a[1]),a[2]);
}
}