simple c++ solution


  • 0
    T
    class Solution {
    public:
        int thirdMax(vector<int>& nums) {
            long long i1 = LLONG_MIN, i2 = LLONG_MIN, i3 = LLONG_MIN;
            for (int i:nums) {
                if (i == i1 || i == i2 || i == i3) continue;
                if (i1 < i) {i3=i2, i2=i1, i1 = i;}
                else if (i2 < i) {i3=i2, i2 = i;}
                else if (i3 < i) i3 = i;
            }
            return i3 != LLONG_MIN ? i3:i1;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.