No.56 Merge Intervals

No.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].

法一:直接复用之前写的insert函数,依次将区间段插入到结果集中

 1 #include "stdafx.h"
 2 #include <vector>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 struct Interval
 8 {
 9     int start;
10     int end;
11     Interval():start(0),end(0) {}
12     Interval(int s, int e):start(s),end(e) {}
13 };
14 class Solution
15 {
16 public:
17     vector<Interval> merge(vector<Interval> &intervals)
18     {//未排序的区间段数组,进行合并
19      //法一:复用之前的insert函数,每次从intervals中取一个区间插入到结果集中
20         vector<Interval> res;
21         for(int i=0; i<intervals.size(); i++)
22             res = insert(res,intervals[i]);
23         return res;
24
25     }
26     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval)
27     {//参考:书
28      //从前向后比较,看是否插入。前提:区间段已排序,无重合
29      //改进之处:方法没变,但由于insert和erase函数代价有点高,会移动修改,故,不做原地的,空间换时间,直接新建一个好了。
30         vector<Interval> res;
31         int count = intervals.size();
32         if(count == 0)
33         {
34             res.push_back(newInterval);//防止待插入区间在最后
35             return res;
36         }
37
38         int index = 0;
39         while(index<count)
40         {
41             if(newInterval.end < intervals[index].start)
42             {//当前区间在待插入区间之前,直接插入待插入区间
43                 res.push_back(newInterval);
44                 while(index<count)
45                 {
46                     res.push_back(intervals[index]);//剩余元素插入res
47                     index++;
48                 }
49                 return res;
50             }
51             else if(newInterval.start > intervals[index].end)
52             {//当前区间大于待插入区间,跳过,继续判断
53                 res.push_back(intervals[index]);
54             }
55             else
56             {//当前区间与待插入区间之间有重合部分
57                 newInterval.start = min(newInterval.start,intervals[index].start);
58                 newInterval.end = max(newInterval.end,intervals[index].end);
59             }
60             index++;
61         }
62         res.push_back(newInterval);//防止待插入区间在最后
63         return res;
64     }
65 };
66 int main()
67 {
68     Solution sol;
69
70     Interval data1[] = {Interval(1,3),Interval(2,6),Interval(8,10),Interval(15,18)};
71     vector<Interval> test1(data1,data1+4);
72 //test1
73     for(auto &i : test1)
74         cout << "["<<i.start << ","<< i.end<<"]";
75     cout << endl;
76     vector<Interval> res1 = sol.insert(test1,Interval(2,5));
77     for(auto &i : res1)
78         cout << "["<<i.start << ","<< i.end<<"]";
79     cout << endl;
80     cout << endl;
81
82     Interval data2[] = {Interval(3,8),Interval(2,9),Interval(6,7),Interval(8,10),Interval(1,2)};
83     vector<Interval> test2(data2,data2+5);
84 //test2
85     for(auto &i : test2)
86         cout << "["<<i.start << ","<< i.end<<"]";
87     cout << endl;
88     vector<Interval> res2 = sol.insert(test2,Interval(4,9));
89     for(auto &i : res2)
90         cout << "["<<i.start << ","<< i.end<<"]";
91     cout << endl;
92
93     return 0;
94 }

法二:将数组自定义排序,然后从前向后两两合并

参考:http://www.cnblogs.com/ganganloveu/p/4158759.html

时间: 2024-10-30 20:38:05

No.56 Merge Intervals的相关文章

LeetCode --- 56. Merge Intervals

题目链接: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]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

56. Merge Intervals - LeetCode

Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并 Java实现: public List<Interval> merge(List<Interval> intervals) { if (intervals == null || intervals.size() == 0) return i

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, mer

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/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]

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge 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]. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0)

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

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]. 思路:题意很明确,首先对各区间按开始来排序,最后遍历,如果前面和后面的区间有重合,合并. 具体代码: /** * Definition for an interval. * publi

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]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序

[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]. 问题:给定一个区间集合,整合所有重叠的区间. 对区间集合按照 start 来排序,然后根据 intervals[i].start 和 res.lastElement.end 来整合即可. 1 int static com

[LeetCode] 56. Merge Intervals Java

题目: 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]. 题意及分析:给出很多个区间,合并有重叠的区间,输出最后的结果.我们分析可以得知对于两个区间interval1和interval2,如果interval1.start<=interval2.start,有一下几种情