~10 line very easy C++ solution! (Should be marked Medium or Easy|)


  • 0
    S

    '''
    class Solution {
    public:

    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval> ret;
        if(intervals.size()==0) return ret;
        sort(intervals.begin(), intervals.end(), [](Interval & a, Interval & b){return a.start < b.start;});
    
        int far = intervals[0].end;
        int st = intervals[0].start;
        for(int i = 1; i< intervals.size(); i++){
            if(far >= intervals[i].start) far = max(far, intervals[i].end);
            else {
                ret.push_back(Interval(st,far));
                st =  intervals[i].start;
                far = intervals[i].end;
            }
        }
        ret.push_back(Interval(st,far));
        return ret;
    }
    

    };
    '''


Log in to reply
 

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