Third Maximum Number
给一个数组, 找第三大的数, 如果不够三个就返回第一个…注意数组中的数字有相同的…
class Solution {
public int thirdMax(int[] nums) {
Integer first = null;
Integer second = null;
Integer third = null;
for(Integer n : nums) {
if(n.equals(first) || n.equals(second) || n.equals(third))
continue;
if(first == null || n > first){
third = second;
second = first;
first = n;
}
else if(second == null || n > second){
third = second;
second = n;
}
else if(third == null || n > third)
third = n;
}
if(third == null){
return first;
}
else
return third;
}
}