Very concise and easy understood C++ O(NlogN) sorting solution


  • 0
    L
    class Solution {
    public:
        static bool comp(Interval a, Interval b) {
            return a.start < b.start;
        }
        vector<Interval> merge(vector<Interval>& intervals) {
            vector<Interval> ret;
            sort(intervals.begin(), intervals.end(), comp);
            for (auto iv : intervals)
                if (ret.empty() || ret.back().end < iv.start)
                    ret.push_back(iv);
                else
                    ret.back().end = max(ret.back().end, iv.end);
            return ret;
        }
    };

Log in to reply
 

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