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;
    }
};

 

posted @ 2019-11-16 21:23  pycodego  阅读(150)  评论(0编辑  收藏  举报