Leetcode---每日一题之56合并区间

#include <iostream>
#include <queue>
#include <functional>
#include <vector>
using namespace std;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals) {
        vector<vector<int>> res;
        sort(intervals.begin(), intervals.end());
        int cur = 0;
        for (const auto &interval : intervals) {
            if (res.empty()) {
                res.push_back(interval);
            }
            else if (interval[0] <= res[cur][1]) {    //可以的话直接替换即可
                if (interval[1] > res[cur][1]) {
                    res[cur][1] = interval[1];
                }
            }
            else {
                res.push_back(interval);
                ++cur;
            }
        }
        return res;
    }
};

猜你喜欢

转载自www.cnblogs.com/z2529827226/p/11873994.html