/**
* 因为求第三大的数,所以需要一个指针存放第三大的
* 如果后面的数大于最大数和第二大的数都需要把最大数,第二大,的数移动
* @param nums
* @return
*/
public static int thirdMax(int[] nums) {
if (nums.length==1){
return nums[0];
}
if (nums.length==2){
return Math.max(nums[0],nums[1]);
}
long max = Long.MIN_VALUE,sec = Long.MIN_VALUE,thrid = Long.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
//去重复
if (nums[i]==max || nums[i]==sec){
continue;
}
if (nums[i]>max){
thrid = sec;
sec = max;
max = nums[i];
}else if (nums[i]>sec && nums[i]!=max){
thrid = sec;
sec = nums[i];
}else if (nums[i]>thrid && nums[i]!=sec){
thrid = nums[i];
}
}
return thrid==Long.MIN_VALUE? (int) max:(int) thrid;
}