[LeetCode]56.Merge Intervals

简介:

【题目】

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

【分析】

(1)先将目标区间数组按X轴从小到大排序。例如:[2,3] [1,2] [3,9] ->[1,2] [2,3] [3,9]

(2)扫描排序后的目标区间数组,将这些区间合并成若干个互不相交的区间。例如 [2,3] [1,2] [4,9] ->[1,3]  [4,9]

这里分三种情况:

a:[1,3] [2,6]  -> [1,6]  第一个区间的end大于等于第二个区间的start,同时第二个区间的end大于第一个区间的end

b:[1,7] [2,4] -> [1,7]  第一个区间的end大于等于第二个区间的start,同时第二个区间的end小于第一个区间的end

c:[1,2] [3,4] -> [1,2] [3,4] 第一个区间的end小于第二个区间的start

【代码】

/*********************************
*   日期:2015-01-14
*   作者:SJF0115
*   题目: 56.Merge Intervals
*   网址:https://oj.leetcode.com/problems/merge-intervals/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

class Solution {
public:
    // 比较函数
    static bool cmp(const Interval& ina,const Interval& inb){
        return ina.start < inb.start;
    }
    vector<Interval> merge(vector<Interval> &intervals) {
        vector<Interval> result;
        int count = intervals.size();
        if(count <= 1){
            return intervals;
        }//if
        // x轴排序
        sort(intervals.begin(),intervals.end(),cmp);
        // 合并
        result.push_back(intervals[0]);
        // 考虑3种情况
        for(int i = 1;i < count;i++){
            Interval preIn = result.back();
            Interval curIn = intervals[i];
            // [1,3] [2,6]
            if(curIn.start <= preIn.end && curIn.end > preIn.end){
                   preIn.end = curIn.end;
                   result.pop_back();
                   result.push_back(preIn);
            }//if
            // [1,2] [3,4]
            else if(curIn.start > preIn.end){
                result.push_back(curIn);
            }
            // [1,7] [2,3] 不用做任何事
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    Interval in1(1,2);
    Interval in2(4,6);
    Interval in3(8,10);
    Interval in4(15,18);

    vector<Interval> vec;
    vec.push_back(in1);
    vec.push_back(in2);
    vec.push_back(in3);
    vec.push_back(in4);
    // 合并
    vector<Interval> v = solution.merge(vec);
    // 输出
    for(int i = 0;i < v.size();i++){
        Interval in = v[i];
        cout<<"["<<in.start<<","<<in.end<<"]"<<endl;
    }//for
    return 0;
}




【温故】

/*---------------------------------------------------------
*   日期:2015-04-21
*   作者:SJF0115
*   题目: 56.Merge Intervals
*   网址:https://oj.leetcode.com/problems/merge-intervals/
*   结果:AC
*   来源:LeetCode
*   博客:
------------------------------------------------------------*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct Interval {
    int start;
    int end;
    Interval() : start(0), end(0) {}
    Interval(int s, int e) : start(s), end(e) {}
};

class Solution {
public:
    // 比较函数
    static bool cmp(const Interval& a,const Interval& b){
        return a.start < b.start;
    }
    vector<Interval> merge(vector<Interval> &intervals) {
        vector<Interval> result;
        int size = intervals.size();
        if(size <= 0){
            return result;
        }//if
        // x轴排序
        sort(intervals.begin(),intervals.end(),cmp);
        // 合并
        result.push_back(intervals[0]);
        for(int i = 1;i < size;++i){
            Interval pre = result.back();
            Interval cur = intervals[i];
            // [1,3] [5,6]
            if(cur.start > pre.end){
                result.push_back(cur);
            }//if
            else{
                int start = min(pre.start,cur.start);
                int end = max(pre.end,cur.end);
                Interval tmp(start,end);
                result.pop_back();
                result.push_back(tmp);
            }//else
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    Interval in1(1,4);
    Interval in2(2,6);
    Interval in3(8,16);
    Interval in4(15,18);

    vector<Interval> vec;
    vec.push_back(in1);
    vec.push_back(in2);
    vec.push_back(in3);
    vec.push_back(in4);
    // 合并
    vector<Interval> v = solution.merge(vec);
    // 输出
    for(int i = 0;i < v.size();i++){
        Interval in = v[i];
        cout<<"["<<in.start<<","<<in.end<<"]"<<endl;
    }//for
    return 0;
}


目录
相关文章
|
存储
LeetCode 352. Data Stream as Disjoint Intervals
给定一个非负整数的数据流输入 a1,a2,…,an,…,将到目前为止看到的数字总结为不相交的区间列表。
151 0
LeetCode 352. Data Stream as Disjoint Intervals
LeetCode 88. Merge Sorted Array
题意是给定了两个排好序的数组,让把这两个数组合并,不要使用额外的空间,把第二个数组放到第一个数组之中.
124 0
LeetCode 88. Merge Sorted Array
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
人工智能 搜索推荐
LeetCode 56. Merge Intervals
给定间隔的集合,合并所有重叠的间隔。
200 0
Leetcode-Easy21. Merge Two Sorted Lists
Leetcode-Easy21. Merge Two Sorted Lists
147 0
Leetcode-Easy21. Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
138 0
|
Java Python
LeetCode 21:合并两个有序链表 Merge Two Sorted Lists
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解题思路: 迭代和递归都能解题。
1049 0
|
Java
[LeetCode]Merge Sorted Array 合并排序数组
链接:https://leetcode.com/problems/merge-sorted-array/description/难度:Easy题目:88.
1082 0

热门文章

最新文章