Cpp Easy Solution


  • 0
    K
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     *     Interval() : start(0), end(0) {}
     *     Interval(int s, int e) : start(s), end(e) {}
     * };
     */
    class Solution {
    public:
        bool static compare(const Interval &a,const Interval &b){
            return a.start<b.start;
        }
        vector<Interval> merge(vector<Interval>& intervals) {
            sort(intervals.begin(),intervals.end(),compare);
            int i,size = intervals.size();
            for(i=1;i<size;){
                if(intervals[i].start <= intervals[i-1].end){
                    intervals[i-1].end = max(intervals[i].end,intervals[i-1].end);
                    intervals.erase(intervals.begin() + i);
                    size--;
                }
                else{
                    i++;
                }
            }
            return intervals;
        }
    };

Log in to reply
 

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