20ms ,My Short C++ solution ,O(nlog(n)) time


  • 0
    H
    class Solution {
    public:
    vector<Interval> merge(vector<Interval>& data) {
        sort(data.begin(),data.end(),
        [](const Interval& a,const Interval & b){return a.start < b.start;});
        auto it = data.begin();
        for(const auto &x:data)
            if(it->end >= x.start)
                it->end = max(x.end,it->end);
            else
                *(++it) = x;
        if(it!=data.end())
            data.erase(++it,data.end());
        return data;
    }
    };

Log in to reply
 

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