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;
    }
};
posted @ 2020-07-07 09:33  景云ⁿ  阅读(49)  评论(0编辑  收藏  举报