leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0414.cpp (869B)
0 class Solution {
1 public:
2 int thirdMax(vector<int> &nums) {
3 long long firstMax = numeric_limits<long long>::min();
4 long long secondMax = numeric_limits<long long>::min();
5 long long thirdMax = numeric_limits<long long>::min();
7 for (int &num : nums) {
8 if (firstMax == num || secondMax == num || thirdMax == num) {
9 continue;
10 }
12 if (firstMax <= num) {
13 thirdMax = secondMax;
14 secondMax = firstMax;
15 firstMax = num;
16 } else if (secondMax <= num) {
17 thirdMax = secondMax;
18 secondMax = num;
19 } else if (thirdMax <= num) {
20 thirdMax = num;
21 }
22 }
24 if (thirdMax == numeric_limits<long long>::min()) return firstMax;
26 return thirdMax;
27 }
28 };