23lines,16ms concise c++ solution,O(n) time and space


  • 0
    Z
     vector<Interval> insert(vector<Interval>& itvs, Interval a) {
            vector<Interval> ret;
            int hasInsert=0;
            for(auto x:itvs){
                if(hasInsert)
                    ret.push_back(x);
                else{
                    if(x.start>a.end){
                        ret.push_back(a);
                        hasInsert=1;
                        ret.push_back(x);
                    }
                    else if(a.start>x.end)
                        ret.push_back(x);
                    else {
                        a.start=min(a.start,x.start);
                        a.end=max(a.end,x.end);
                    }
                }
            }
            if(!hasInsert)ret.push_back(a);
            return ret;
        }
    

Log in to reply
 

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