LeetCode+ 56 - 60

合并区间

LeetCode+ 56 - 60_第1张图片

双指针算法、位运算、离散化、区间合并_小雪菜本菜的博客-CSDN博客

class Solution {
public:
    vector> merge(vector>& a) {
        vector> res;
        if(a.empty()) return res;

        sort(a.begin(),a.end());
        int l = a[0][0],r = a[0][1];
        for(int i = 0;i < a.size();i++ ) {
            if(a[i][0] > r) {
                res.push_back({l,r});
                l = a[i][0],r = a[i][1];
            } else r = max(r,a[i][1]);
        }
        res.push_back({l,r});
        return res;
    }
};

插入区间

你可能感兴趣的:(LeetCode+,leetcode,算法,职场和发展)