simple 10 lines c++ solution


  • 0
    M
    int thirdMax(vector<int>& nums) {
            set<int> s;
            for(int i = 0; i < nums.size(); ++i) {
                s.insert(nums[i]);
                if(s.size() > 3) s.erase(s.begin());
            }
            if(s.size() < 3) return *(--s.end());
            return *s.begin();
        }
    

Log in to reply
 

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