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;
    }
};
相关推荐
To_OC11 小时前
LC 17 电话号码的字母组合:我的回溯算法,就是从这道题开窍的
javascript·算法·leetcode
blueman888812 小时前
Qt5通过vcpkg中调用时,在debug模式下调试时总是调用release的plugins文件夹中的dll
c++·qt·cmake
我是坏垠13 小时前
Crypto、Cipher与Password:Java加密开发的三个核心概念
java·开发语言·python
white_ant13 小时前
JDK LTS 版本升级迁移注意事项大全
java·开发语言
Bobolink_13 小时前
跨境业务网络环境评估,“干净、一致、独享”三个关键指标
开发语言·网络·php·跨境网络·网络环境
你怎么知道我是队长14 小时前
JavaScript的变量和数据类型介绍
开发语言·javascript·ecmascript
xingke14 小时前
C 语言多文件编程:(一)头文件、extern、编译链接
c语言·开发语言·多文件
戮漠summer14 小时前
Missing Semester 计算机教育中缺失的一课 Lecture 01 Shell
开发语言·后端·scala
jinyishu_14 小时前
C++ string使用方法
开发语言·c++
EW Frontier15 小时前
三级跳突破864维动作空间——QMIX-Hierarchical多无人机协同通信方法全解析【附python代码】
开发语言·python·无人机·强化学习·通信资源分配