leetcode 56. 合并区间

发布于:2025-05-20 ⋅ 阅读:(13) ⋅ 点赞:(0)

题目描述

代码:

先将所给区间按照区间左端点从小到大排序。排完序之后,可以合并的区间一定是相邻的,只需要按顺序遍历一遍即可解决。

class Solution {
    struct Interval{
        int left;
        int right;
        Interval(int l=0,int r=0):left(l),right(r){}
        bool operator<(const Interval& rhs) const{
            return left<rhs.left;
        }
    };

public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        int len = intervals.size();
        vector<Interval> myIntervals(len);
        for(int i = 0;i <len;i++){
            myIntervals[i].left =intervals[i][0];
            myIntervals[i].right=intervals[i][1];
        }
        sort(myIntervals.begin(),myIntervals.end());
        vector<vector<int>> res;
        vector<int> range(2,0);
        int low = 0;
        int high = 0;
        for(int i = 1;i < len;i++){
            if(myIntervals[high].right >= myIntervals[i].right)
                continue;

            if(myIntervals[high].right >= myIntervals[i].left){
                high = i;
            }else{
                range[0] = myIntervals[low].left;
                range[1] = myIntervals[high].right;
                res.push_back(range);
                low = i;
                high = low;
            }
        }
        range[0] = myIntervals[low].left;
        range[1] = myIntervals[high].right;
        res.emplace_back(range);
        return res;
    }
};

实际上vector支持比较操作符,可以不用像上面代码那样构造一个可以比较大小的Interval类型。

写几个示例看看效果:

#include<vector>
#include<iostream>
using namespace std;

int main()
{
    vector<int> nums1 = {1,2,3};
    vector<int> nums2 = {1,2,3}; 
    cout<<boolalpha<<(nums1==nums2)<<endl;//输出true
    vector<int> nums3 = {1,2,3};
    vector<int> nums5 = {4,5,6};
    cout<<boolalpha<<(nums3 <nums5)<<endl;//输出true
    cout<<boolalpha<<(nums3==nums5)<<endl;//输出false
    cout<<boolalpha<<(nums3 >nums5)<<endl;//输出false
    cout<<"----------------------"<<endl;
    vector<vector<int>> vec1{{1,2},{3,4}};
    vector<vector<int>> vec2{{1,2},{3,4}};
    cout<<boolalpha<<(vec1==vec2)<<endl;//输出true
    cout<<boolalpha<<(vec1 <vec2)<<endl;//输出false
    cout<<"----------------------"<<endl;
    vector<vector<int>> vec3{{1,2},{3,4}};
    vector<vector<int>> vec4{{5,6},{7,8}};
    cout<<boolalpha<<(vec3==vec4)<<endl;//输出false
    cout<<boolalpha<<(vec3 <vec4)<<endl;//输出true
    cout<<"----------------------"<<endl;
    vector<vector<int>> vec5{{1,2},{3,4,5},{6}};
    vector<vector<int>> vec6{{1,2},{3,4},{5,6}};
    cout<<boolalpha<<(vec5==vec6)<<endl;//输出false
    cout<<boolalpha<<(vec5 <vec6)<<endl;//输出false
    cout<<boolalpha<<(vec5 >vec6)<<endl;//输出true
    return 0;
}

对本题中的区间排序试试看:

#include<vector>
#include<iostream>
using namespace std;

int main()
{
    vector<vector<int>> intervals = {{2,6},{8,10},{1,3},{7,9},{15,18}};
    for(auto &interval:intervals){
        cout<<"["<<interval[0]<<","<<interval[1]<<"],";
    }
    cout<<endl;
    
    sort(intervals.begin(),intervals.end());
    for(auto &interval:intervals){
        cout<<"["<<interval[0]<<","<<interval[1]<<"],";
    }
    cout<<endl;
    return 0;
}

输出:

[2,6],[8,10],[1,3],[7,9],[15,18],
[1,3],[2,6],[7,9],[8,10],[15,18],

按照这个做法,更简洁的答案是:

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> res;
        int len = intervals.size();
        sort(intervals.begin(),intervals.end());
        res.push_back(intervals[0]);//题目已经保证1 <= intervals.length <= 104
        for(int i =1;i <len;i++){
            if(res.back()[1] >= intervals[i][0]){
                res.back()[1] = max(res.back()[1],intervals[i][1]);
            }else{
                res.push_back(intervals[i]);
            }
        }
        return res;
    }
};

网站公告

今日签到

点亮在社区的每一天
去签到