题目链接:https://leetcode-cn.com/problems/merge-intervals/
题目描述:
题解:
class Solution {
public:
static bool cmp(vector<int> &a, vector<int> &b)
{
return a[0] < b[0];
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> result;
if(intervals.size() < 2)
return intervals;
sort(intervals.begin(), intervals.end(), cmp); //按左边界排序
result.push_back(intervals[0]);
for(int i = 1; i < intervals.size(); i++)
{
if(result.back()[1] >= intervals[i][0])
{
result.back()[1] = max(result.back()[1], intervals[i][1]); //更新边界值
}else
{
result.push_back(intervals[i]);
}
}
return result;
}
};