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;
    }
};
相关推荐
同学小张25 分钟前
【端侧AI 与 C++】1. llama.cpp源码编译与本地运行
开发语言·c++·aigc·llama·agi·ai-native
2501_9416233226 分钟前
智慧农业监控平台中的多语言语法引擎与实时决策实践
leetcode
踢球的打工仔1 小时前
PHP面向对象(7)
android·开发语言·php
汤姆yu3 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
Yue丶越4 小时前
【C语言】字符函数和字符串函数
c语言·开发语言·算法
翔云 OCR API4 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
小白程序员成长日记4 小时前
2025.11.24 力扣每日一题
算法·leetcode·职场和发展
有一个好名字4 小时前
LeetCode跳跃游戏:思路与题解全解析
算法·leetcode·游戏
V***u4534 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿5 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台