/*
* @lc app=leetcode.cn id=414 lang=typescript
*
* [414] 第三大的数
*/
//一次遍历
// @lc code=start
function thirdMax(nums: number[]): number {
let first = -Infinity,
second = -Infinity,
third = -Infinity;
for (let i = 0; i < nums.length; i++) {
//大于最大值则依次换值
if (nums[i] > first) {
third = second;
second = first;
first = nums[i];
}
//考虑最大值不能等于第二大值
else if (nums[i] > second && nums[i] < first) {
third = second;
second = nums[i];
}
///考虑第二大值不能等于第三大值
else if (nums[i] > third && nums[i] < second) {
third = nums[i];
}
}
return third === -Infinity ? first : third;
}
// @lc code=end