My thought is to sort the intervals firstly, and then merge it.

```
bool cmp(Interval a, Interval b) {
if (a.start != b.start) return a.start < b.start;
else return a.end < b.end;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
if (intervals.size() < 2) return intervals;
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval>::iterator iter;
iter = intervals.begin();
for (int i = 0; i < intervals.size() - 1; ) {
if (intervals[i + 1].start <= intervals[i].end) {
intervals[i + 1].start = intervals[i].start;
if (intervals[i + 1].end < intervals[i].end) intervals[i + 1].end = intervals[i].end;
intervals.erase(iter);
} else {
i++;
iter++;
}
}
return intervals;
}
};
```