Code with sort().


  • 0
    Z
    class Solution {
        static bool checkinterval (const Interval &a,const Interval &b)
        {
            return a.start<b.start?true:false;
        }
    public:
        vector<Interval> merge(vector<Interval>& intervals) {
            int size = intervals.size();
            if(size <=1) return intervals;
            sort(intervals.begin(),intervals.end(),checkinterval);
            vector<Interval> res;
            //res.push_back(intervals[0]);
            Interval expand = intervals[0];
            for(int i = 1;i<size;++i)
            {
                if(intervals[i].start<=expand.end)
                expand.end = max(intervals[i].end,expand.end);
                else
                {
                    res.push_back(expand);
                    expand = intervals[i];
                }
            }
            res.push_back(expand);
            return res;
        }
    };

Log in to reply
 

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