forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThirdMaximumNumber.java
57 lines (44 loc) · 1.61 KB
/
ThirdMaximumNumber.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package easy;
public class ThirdMaximumNumber {
public static int thirdMax_20161115(int[] nums) {
if (nums == null || nums.length == 0) return 0;
if (nums.length < 3) {
int max = 0;
for (int i : nums) {
max = Math.max(i, max);
}
return max;
}
long first = Long.MIN_VALUE, second = Long.MIN_VALUE, third = Long.MIN_VALUE;
for (int i = 0; i < nums.length; i++){
if (nums[i] > first) {
long tmpFirst = first;
first = nums[i];
long tmpSecond = second;
second = tmpFirst;
third = tmpSecond;
} else if (nums[i] == first) continue;
else if (nums[i] > second) {
long tmpSecond = second;
second = nums[i];
third = tmpSecond;
} else if (nums[i] == second) continue;
else if (nums[i] > third) {
third = nums[i];
}
}
if (third == Long.MIN_VALUE){
return (int) first;
}
return (int) third;
}
public static void main(String...strings){
// int[] nums = new int[]{1,2};
// int[] nums = new int[]{2,2,3,1};
// int[] nums = new int[]{1,1,2};//should be 2
int[] nums = new int[]{1,2,-2147483648};//should be -2147483648
// int[] nums = new int[]{3,2,1};
// System.out.println(thirdMax(nums));
System.out.println(thirdMax_20161115(nums));
}
}