https://leetcode-cn.com/problems/merge-intervals/
思路:sort排序后得到区间最小值,用o(n)遍历一次进行合并即可
vector<vector<int> > merge(vector<vector<int> >& intervals) {
if (intervals.size() <= 1) {
return intervals;
}
vector<vector<int> > res;
sort(intervals.begin(), intervals.end());
int i = 0;
while(i < intervals.size()) {
int j = i + 1;
//用t来存放intervals[I]值,不用单独判断数组最后一组是否被使用
int t = intervals[i][1];
while(j < intervals.size()) {
//区间不重叠
if (intervals[j][0] > t) {
break;
//区间重叠,进行合并
} else {
t = max(t, intervals[j][1]);
}
j += 1;
}
intervals[i][1] = t;
res.push_back(intervals[i]);
i = j;
}
return res;
}