C++速通LeetCode中等第9题-合并区间

排序后迭代,遇到符合条件的就删除前一项,合并到后一项。

cpp 复制代码
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        int left = 0,right = 0;
        sort(intervals.begin(), intervals.end());
        vector<int> tmp;
        for(auto it = intervals.begin(); it != --(intervals.end()); )
        {
            if((*it)[1] >= (*(++it))[0])
            {
                left = (*(--it))[0];
                right = (*it)[1];
                it = intervals.erase(it);
                right = max(right,(*it)[1]);
                tmp.push_back(left);
                tmp.push_back(right);
                *(it) = tmp;
                tmp.clear();
            }
        }
        return intervals;
    }
};
相关推荐
肆忆_7 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星11 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
琢磨先生David4 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈20214 天前
信号量和信号
linux·c++