Problem:
Algorithm:
策略:每加入一个元素,判断这个元素1st_max? 2nd_max? 3rd_max; 判断方式如代码所示
注意⚠️:return 判断是否是long最小值用于返回没有3rd_max的情况,此时返回1st_max;
Java :
public class Solution
{
public int thirdMax(int[] nums)
{
long first=Long.MIN_VALUE;
long second=Long.MIN_VALUE;
long third=Long.MIN_VALUE;
for(int i:nums){
if(i>first){
third=second;
second=first;
first=i;
}else if(i==first)
continue;
else if(i>second){
third=second;
second=i;
}else if(i==second)
continue;
else if(i>third){
third=i;
}
}
return third==Long.MIN_VALUE?(int)first:(int)third;
}
}