题目:
代码(首刷自解 2024年2月18日):
这题与气球扎针,删除重复的大体逻辑相似。需要额外定义些变量来存储头尾
class Solution {
private:const static bool cmp(vector<int>& a, vector<int>& b) {return a[0] < b[0];}
public:vector<vector<int>> merge(vector<vector<int>>& intervals) {if (intervals.size() < 2) return intervals;sort(intervals.begin(), intervals.end(), cmp);vector<vector<int>> res;int head = intervals[0][0];int end = intervals[0][1];for (int i = 1; i < intervals.size(); ++i) {if (intervals[i][0] <= end) {end = max(intervals[i][1],end);} else {res.push_back(vector<int>{head,end});head = intervals[i][0];end = intervals[i][1];}}res.push_back(vector<int>{head,end});return res;}
};