0414.cpp (869B)
1 class Solution { 2 public: 3 int thirdMax(vector<int> &nums) { 4 long long firstMax = numeric_limits<long long>::min(); 5 long long secondMax = numeric_limits<long long>::min(); 6 long long thirdMax = numeric_limits<long long>::min(); 7 8 for (int &num : nums) { 9 if (firstMax == num || secondMax == num || thirdMax == num) { 10 continue; 11 } 12 13 if (firstMax <= num) { 14 thirdMax = secondMax; 15 secondMax = firstMax; 16 firstMax = num; 17 } else if (secondMax <= num) { 18 thirdMax = secondMax; 19 secondMax = num; 20 } else if (thirdMax <= num) { 21 thirdMax = num; 22 } 23 } 24 25 if (thirdMax == numeric_limits<long long>::min()) return firstMax; 26 27 return thirdMax; 28 } 29 };