码迷,mamicode.com
首页 > 其他好文 > 详细

No.56 Merge Intervals

时间:2015-06-09 13:29:51      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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

No.56 Merge Intervals

标签:

原文地址:http://www.cnblogs.com/dreamrun/p/4562922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!