-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (30 loc) · 927 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
38
39
package thirdmaximumnumber;
class Solution {
public static int thirdMax(int[] nums) {
Integer max1 = nums[0];
Integer max2 = null;
Integer max3 = null;
for (int i=1; i<nums.length; i++) {
if (nums[i] > max1) {
max3 = max2;
max2 = max1;
max1 = nums[i];
} else if((max2 == null || nums[i] > max2) && nums[i] < max1) {
if (max3 != max2) {
max3 = max2;
}
max2 = nums[i];
} else if((max3 == null || nums[i] > max3) && (max2 != null && nums[i] < max2)) {
max3 = nums[i];
}
}
if (max3 != null) {
return max3;
}
return max1;
}
public static void main(String[] args) {
int[] arr = new int[]{2,2,3,1};
System.out.println(thirdMax(arr));
}
}