-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThirdMaximumNumber.java
More file actions
52 lines (43 loc) · 1.32 KB
/
ThirdMaximumNumber.java
File metadata and controls
52 lines (43 loc) · 1.32 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
44
45
46
47
48
49
50
51
52
package Leetcode;
import java.util.ArrayList;
import java.util.List;
public class ThirdMaximumNumber
{
public int thirdMax( int[] nums )
{
if( nums == null || nums.length == 0 )
return 0;
Integer max = null;
Integer secondMax = null;
Integer thirdMax = null;
for( Integer num : nums )
{
if( ( max != null && max.equals( num )) || ( secondMax != null && secondMax.equals( num ))
|| ( thirdMax != null && thirdMax.equals( num ) ))
continue;
if( max == null || num > max )
{
thirdMax = secondMax;
secondMax = max;
max = num;
}
else if( secondMax == null || num > secondMax )
{
thirdMax = secondMax;
secondMax = num;
}
else if( thirdMax == null || num > thirdMax )
{
thirdMax = num;
}
}
if( secondMax == null || thirdMax == null )
return max;
return thirdMax;
}
public static void main( String[] args )
{
ThirdMaximumNumber obj = new ThirdMaximumNumber();
System.out.println( obj.thirdMax( new int[] {-2147483648,-2147483648,-2147483648,-2147483648,1,1,1} ) );
}
}