Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18]
,
return [1,6],[8,10],[15,18]
.
问题:给定一个区间集合,整合所有重叠的区间。
对区间集合按照 start 来排序,然后根据 intervals[i].start 和 res.lastElement.end 来整合即可。
int static comp(Interval t1, Interval t2){
int res = (t2.start > t1.start);
return res;
} vector<Interval> merge(vector<Interval>& intervals) { vector<Interval> vt; if (intervals.size() == ) {
return vt;
} std::sort(intervals.begin(), intervals.end(), comp); vt.push_back(intervals[]); for (int i = ; i < intervals.size(); i++) { if (intervals[i].start <= vt.back().end) {
vt.back().end = max( vt.back().end, intervals[i].end);
}else{
vt.push_back(intervals[i]);
}
} return vt; }