class Solution {
public:
int thirdMax(vector<int>& nums) {
if (nums.size() == 1) {
return nums[0];
}
if (nums.size() == 2) {
return max(nums[0], nums[1]);
}
long n1 = LONG_MIN;
long n2 = LONG_MIN;
long n3 = LONG_MIN;
for (auto &num : nums) {
if (num > n1) {
n3 = n2;
n2 = n1;
n1 = num;
}
else if ( num < n1 && num > n2) {
n3 = n2;
n2 = num;
}
else if (num < n1 && num < n2 && num > n3) {
n3 = num;
}
}
return n3 == LONG_MIN ? n1 : n3;
}
};
414-第三大的数
最新推荐文章于 2022-08-06 17:59:32 发布