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 };


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

 

 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     //自定义的比较函数,为使sort调用,必须是全局或者是静态的,不能是普通成员函数
18     static bool compare(Interval v1, Interval v2)
19     {
20         return v1.start==v2.start ? v1.end<v2.end : v1.start<v2.start;
21     }
22     vector<Interval> merge(vector<Interval> &intervals)
23     {//未排序的区间段数组,进行合并
24      //法二:先将区间段数组排序,然后从前向后两两合并
25      //参考:http://www.cnblogs.com/ganganloveu/p/4158759.html
26         
27         vector<Interval> res;//结果集
28         int count = intervals.size();
29         if(count == 0)
30             return res;
31         //区间段排序
32         sort(intervals.begin(),intervals.end(),compare);
33         res.push_back(intervals[0]);
34 
35         for(int i=1; i<count; i++)
36         {//依次插入当前区间段,将当前区间段与结果集res的最后区间段进行比较,看是否合并——直接修改数据即可
37             Interval &back = res.back();//back要是引用,否则不会改变其值!!!
38             if(intervals[i].start > back.end)//无重合
39                 res.push_back(intervals[i]);
40             else
41             //有重合
42                 back.end = max(back.end, intervals[i].end);
43         }
44         return res;
45     }
46 };
47 int main()
48 {
49     Solution sol;
50 
51     Interval data1[] = {Interval(1,3),Interval(2,6),Interval(8,10),Interval(15,18)};
52     vector<Interval> test1(data1,data1+4);
53 //test1
54     for(auto &i : test1)
55         cout << "["<<i.start << ","<< i.end<<"]";
56     cout << endl;
57     vector<Interval> res1 = sol.merge(test1);
58     for(auto &i : res1)
59         cout << "["<<i.start << ","<< i.end<<"]";
60     cout << endl;
61     cout << endl;
62 
63     Interval data2[] = {Interval(3,8),Interval(2,9),Interval(6,7),Interval(8,10),Interval(1,2)};
64     vector<Interval> test2(data2,data2+5);
65 //test2
66     for(auto &i : test2)
67         cout << "["<<i.start << ","<< i.end<<"]";
68     cout << endl;
69     vector<Interval> res2 = sol.merge(test2);
70     for(auto &i : res2)
71         cout << "["<<i.start << ","<< i.end<<"]";
72     cout << endl;
73 
74     return 0;
75 }

 

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

posted @ 2015-06-09 11:16  人生不酱油  阅读(172)  评论(0编辑  收藏  举报