设置测试用例的也太恶心了,居然以-2147483648作为输入,而且还放在第二个位置,导致程序很难识别究竟是输入的MIN还是本来就有的MIN。
为了简单起见,想了一个办法,把所有的MIN输入值改为MIN+2,这样就能把输入MIN的和初始化的MIN区分开。
class Solution {
public int thirdMax(int[] nums) {
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
for(int i =0; i< nums.length; i++)
nums[i] = nums[i] == Integer.MIN_VALUE? nums[i]+2: nums[i];
for(int num : nums)
{
if(num > max1)
{
max3 = max2;
max2 = max1;
max1 = num;
}
else if(num < max1 && num > max2)
{
max3 = max2;
max2 = num;
}
else if(num < max2 && (num > max3))
{
max3 = num;}
}
if(max3 == Integer.MIN_VALUE)
max3 = max1;
if(max3 == Integer.MIN_VALUE+2)
max3 = Integer.MIN_VALUE;
return max3;
}
}