[6ms 13 lines] Concise Easy-understanding C++ Insertion Sort


  • 0
    H
    class Solution {
    public:
        int thirdMax(vector<int>& nums) {
            vector<long> cands { (long)INT_MIN - 1, (long)INT_MIN - 1, (long)INT_MIN - 1 };
            
            for (long it : nums) {
                if (it == cands[0] || it == cands[1] || it == cands[2]) continue;
                
                if (it < cands[2]) continue;
                for (auto iit = cands.begin(); iit != cands.end(); ++iit) {
                    if (it > *iit) {
                        cands.insert(iit, it);
                        break;
                    }
                }
                cands.pop_back();
            }
            
            return (cands[2] == (long)(INT_MIN) - 1) ? (int)cands[0] : (int)cands[2];
        }
    };
    

Log in to reply
 

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