Leetcode 414. Third Maximum Number | | Leetcode 414. Third Maximum Number 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 1234567891011121314151617181920212223242526272829class 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 123456789101112class 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 https://leetcode.com/problems/third-maximum-number/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏