LeetCode+ 56 - 60

合并区间

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

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& a) {
        vector<vector<int>> 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;
    }
};

插入区间

猜你喜欢

转载自blog.csdn.net/weixin_60569662/article/details/125402034
今日推荐