leetcode-----56. 合并区间

代码

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ans;
        if (intervals.empty()) return ans;

        sort(intervals.begin(), intervals.end());
        int l = intervals[0][0], r = intervals[0][1];
        for (int i = 1; i < intervals.size(); ++i) {
            if (intervals[i][0] > r) {
                ans.push_back({l, r});
                l = intervals[i][0], r = intervals[i][1];
            } else r = max(r, intervals[i][1]);
        }
        ans.push_back({l, r});
        return ans;
    }
};
上一篇:删除重叠的区间的个数


下一篇:LeetCode-56 合并区间