9 lines, c++, 580 ms, easy to understand


  • 0
    T
    bool static compare( Interval i1,  Interval i2){
        return i1.start == i2.start ? i1.end < i2.end:i1.start < i2.start;
    }
    
    vector<Interval> merge(vector<Interval>& intervals) {
        if(intervals.size() < 2) return intervals;
        
        sort(intervals.begin(),intervals.end(),compare);
        vector<Interval> rtn={intervals[0]};
    
        for(int i = 1;i < intervals.size();i++)
            if(rtn[rtn.size()-1].end >= intervals[i].start)
                rtn[rtn.size()-1].end = max(intervals[i].end, rtn[rtn.size()-1].end);
            else rtn.push_back(intervals[i]);
        
        return rtn;
    }

Log in to reply
 

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