3ms CPP sol


  • 0

    Talk is cheap, show you the code.

    class Solution {
    public:
        int thirdMax(vector<int>& nums) {
            if  (nums.size() < 3) {
                int maxR = INT_MIN;
                for (auto n : nums) maxR = max(maxR, n);
                return maxR;
            }
            
            int top3[3] = {INT_MIN, INT_MIN, INT_MIN,};
            for (auto n : nums) {
                if (n > top3[0] && n != top3[0] && n != top3[1] && n != top3[2]) {
                    top3[0] = n;
                    if (top3[0] > top3[1])
                        swap(top3[0], top3[1]);
                    if (top3[1] > top3[2])
                        swap(top3[1], top3[2]);
                }
            }
            return top3[0];
        }
    };
    

Log in to reply
 

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