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;
    }
};
相关推荐
困死,根本不会4 小时前
Kivy+Buildozer 打包 APK 踩坑:python-for-android 克隆失败
开发语言·php·kivy
咸鱼2.07 小时前
【java入门到放弃】跨域
java·开发语言
skiy7 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
一念春风7 小时前
智能文字识别工具(AI)
开发语言·c#·wpf
桦08 小时前
【C++复习】:继承
开发语言·c++
_日拱一卒8 小时前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
何仙鸟8 小时前
GarmageSet下载和处理
java·开发语言
鱼难终8 小时前
类和对象(下)
c++
wefly20178 小时前
免安装!m3u8live.cn在线 M3U8 播放器,小白也能快速上手
java·开发语言·python·json·php·m3u8·m3u8在线转换
云泽8088 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法