6 lines C++


  • 0
    class Solution {
    public:
        vector<Interval> merge(vector<Interval>& intervals) {
            sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b){ return a.start < b.start; });
            vector<Interval>res;
            for(int i = 0; i < intervals.size(); i++)
                if(res.empty() || res.back().end < intervals[i].start) res.push_back(intervals[i]);
                else res.back().end = max(res.back().end, intervals[i].end);
            return res;
        }
    };
    

Log in to reply
 

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