Leetcode 414. Third Maximum Number
程序员文章站
2022-03-04 19:00:34
...
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
- Version 1
class Solution {
public:
int thirdMax(vector<int>& nums) {
long long first = LLONG_MIN;
long long second = LLONG_MIN;
long long third = LLONG_MIN;
for(int i = 0; i < nums.size(); i++) {
if(nums[i] <= third || nums[i] == first || nums[i] == second) {
continue;
}
if(nums[i] > first) {
third = second;
second = first;
first = nums[i];
}
else if(nums[i] > second) {
third = second;
second = nums[i];
}
else {
third = nums[i];
}
}
if(third == LLONG_MIN) {
return first;
}
return third;
}
};
- Version 2
class Solution {
public:
int thirdMax(vector<int>& nums) {
set<int> values(nums.begin(), nums.end());
if(values.size() < 3) {
return *values.rbegin();
}
values.erase(--values.end());
values.erase(--values.end());
return *(--values.end());
}
};
Reference
上一篇: 监控与管理-SpringBoot
下一篇: 程序自我更新之发布
推荐阅读
-
Leetcode 1456. Maximum Number of Vowels in a Substring of Given Length (python)
-
【Leetcode】1072. Flip Columns For Maximum Number of Equal Rows(异或运算)
-
#leetcode#321.Create Maximum Number
-
Leetcode 321. Create Maximum Number
-
【Leetcode】414.Third Maximum Number
-
[LeetCode]414. Third Maximum Number
-
414. Third Maximum Number
-
【Array-easy】414. Third Maximum Number 找到数组中第三大的数
-
414. Third Maximum Number
-
[email protected]_414_Third_Maximum_Number