class Solution {
public int thirdMax(int[] nums) {
Set<Integer> set = new HashSet<>();
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE, third = Integer.MIN_VALUE;
for (int num: nums) {
if (set.contains(num) == false) {
if (set.size() == 0) first = num;
else if (set.size() == 1) {
second = first > num ? num : first;
first = first > num ? first : num;
}
else if (set.size() == 2) {
if (num > first) { third = second; second = first; first = num; }
else if (num > second) { third = second; second = num; }
else third = num;
}
else {
if (num > first) { third = second; second = first; first = num; }
else if (num > second) { third = second; second = num; }
else if (num > third) { third = num; }
}
set.add(num);
}
}
return set.size() > 2 ? third : first;
}
}
class Solution {
public int thirdMax(int[] nums) {
long first = Long.MIN_VALUE, second = Long.MIN_VALUE, third = Long.MIN_VALUE;
for (int num: nums) {
if (num > first) { third = second; second = first; first = num; }
else if (first > num && num > second) { third = second; second = num; }
else if (second > num && num > third) third = num;
}
return third == Long.MIN_VALUE ? (int)first : (int)third;
}
}