LeetCode-56. 合并区间

56. 合并区间

难度中等215收藏分享切换为英文关注

通过次数

37,436

提交次数

95,681

题目描述

评论 (344)

题解(93)New

提交记录

给出一个区间的集合,请合并所有重叠的区间。

扫描二维码关注公众号,回复: 8577397 查看本文章

示例 1:

输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

先排序,再合并,这段代码写的不好,效率有点低呢....


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

class Solution{
public:
    vector<vector<int>> merge(vector<vector<int>> &intervals){
        sort(intervals.begin(),intervals.end(),cmp_func);                      /* 排序 */
        for(int i=1;i<intervals.size();i++){                                   /* 合并 */

            //ShowInterVals(intervals);

            vector<int> tempfront = intervals[i-1];
            vector<int> temp = intervals[i];
            int Last;

            if(temp.front() <= tempfront.back()){
                if(temp.back() > tempfront.back()){
                    Last = temp.back();
                }else{
                    Last = tempfront.back();
                }

                cout<<"temp.front:"<<temp.front()<<"---(tempfront).front():"<<tempfront.front()<<" Last:"<<Last<<endl;

                intervals[i].front() = tempfront.front();
                intervals[i].back() = Last;
                intervals.erase(intervals.begin() + i - 1);

                //ShowInterVals(intervals);

                /* 如果当前已经合并了,则i--,将当前合并的结果与新区间再进行一次比较 */
                i--;
            }
        }
        return intervals;
    }


    /* 打印函数,提交前注释 */
    void ShowInterVals(vector<vector<int>> &intervals){
        cout<<"new:"<<endl;
        vector<vector<int>>::iterator CurArr = intervals.begin();
        while(CurArr!=intervals.end()){
            cout<<"arr:";
            vector<int>::iterator it =  (*CurArr).begin();
            while(it!=(*CurArr).end()){
                cout<<*it<<" ";
                it++;
            }
            cout<<endl;
            CurArr++;
        }
    }

private:
    static bool cmp_func(vector<int> &x1, vector<int> &x2){
        int first = x1.front();
        int second = x2.front();
        return (first < second);
    }
};

/* [1,4],[0,2],[3,5] */

/*
 * 输入:[[2,3],[2,2],[3,3],[1,3],[5,7],[2,2],[4,6]]
 * 输出:[[4,6],[4,7]]
 * 预期:[[1,3],[4,7]]
 */

/* * 输入:
 *
 * [[1,3],[2,2],[2,2],[2,3],[3,3],[4,6],[5,7]]
 * [[1,3],[2,2],[2,3],[3,3],[4,6],[5,7]]
 * [[1,3],[2,3],[3,3],[4,6],[5,7]]
 * [[1,3],[3,3],[4,6],[5,7]]
 *
 *
 */

int main(){
    //    vector<int> a1 = {15,18};
    //    vector<int> a2 = {8,10};
    //    vector<int> a3 = {1,3};
    //    vector<int> a4 = {2,6};

    //    vector<int> a1 = {3,5};
    //    vector<int> a2 = {0,2};
    //    vector<int> a3 = {1,4};

    vector<vector<int>> arr = {{2,3},{2,2},{3,3},{1,3},{5,7},{2,2},{4,6}};
    //    arr.push_back(a1);
    //    arr.push_back(a2);
    //    arr.push_back(a3);
    //    arr.push_back(a4);

    Solution* ps = new Solution();
    vector<vector<int>> intervals = ps->merge(arr);

    return 0;
}
发布了245 篇原创文章 · 获赞 57 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/qq_16542775/article/details/103122548