### LeetCode  • ㊗️
• 大家
• offer
• 多多！

## Problem

Given integer array nums, return the third maximum number in this array. If the third maximum does not exist, return the maximum number.

Example 1:

Input: nums = [3,2,1]
Output: 1
Explanation: The third maximum is 1.


Example 2:

Input: nums = [1,2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.


Example 3:

Input: nums = [2,2,3,1]
Output: 1
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.


## Code

628. Maximum Product of Three Numbers

class Solution {
public int thirdMax(int[] nums) {
// [1,2,-2147483648]
long max = Long.MIN_VALUE;
long max2 = Long.MIN_VALUE;
long max3 = Long.MIN_VALUE;

for(long num : nums) {
if(num == max || num == max2 || num == max3) continue;
if(num > max) {
max3 = max2;
max2 = max;
max = num;
} else if (num > max2) {
max3 = max2;
max2 = num;
} else if (num > max3) {
max3 = num;
}
}

return max3 == Long.MIN_VALUE ? (int)max : (int)max3;
}
}