/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
#include<algorithm>
    bool cmp(Interval a, Interval b) {
        if (a.start == b.start) {
            return a.end < b.end;
        }
        return a.start < b.start;
    }
class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        int len = intervals.size();
        vector<Interval> ret;
        if (len == 0) {
            return ret;
        }
        sort(intervals.begin(), intervals.end(), cmp);
        for (int i = 1; i < len; i++) {
            if (intervals[i].start <= intervals[i-1].end) {
                intervals[i].start = min (intervals[i].start, intervals[i-1].start);
                intervals[i].end = max (intervals[i].end, intervals[i-1].end);
            } else {
                ret.push_back(intervals[i-1]);
            }
        }
        // 最后一个要push进去是因为前面在不断的改变当前遍历的值,前面保障了最后一个参数和之间是隔离的
        ret.push_back(intervals[len-1]);
        return ret;
    }
};