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在原区间数组上操作,不使用额外的空间,但是需要删除多余的区间,这样会移动数组元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class
Solution {
private
:
static
bool
comp(Interval a, Interval b)
{
return
a.start < b.start;
}
public
:
vector<Interval> merge(vector<Interval> &intervals) {
if
(intervals.empty())
return
intervals;
sort(intervals.begin(), intervals.end(), comp);
vector<Interval>::iterator it1 = intervals.begin(), it2 = it1 + 1;
while
(it1 != intervals.end() && it2 != intervals.end())
{
if
(it2->start <= it1->end)
{
if
(it1->end < it2->end)it1->end = it2->end;
it2++;
}
else
{
//[it1+1, it2)范围内的区间可以从原数组删除
it1 = intervals.erase(it1+1, it2);
it2 = it1 + 1;
}
}
if
(it1 != intervals.end())
intervals.erase(it1 + 1, it2);
return
intervals;
}
};
|
代码2用新数组来存储合并后的区间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class
Solution {
private
:
static
bool
comp(Interval a, Interval b)
{
return
a.start < b.start;
}
public
:
vector<Interval> merge(vector<Interval> &intervals) {
if
(intervals.empty())
return
intervals;
sort(intervals.begin(), intervals.end(), comp);
vector<Interval> res;
res.push_back(intervals[0]);
for
(
int
i = 1; i < intervals.size(); i++)
{
Interval &p = res.back();
if
(intervals[i].start > p.end)res.push_back(intervals[i]);
else
if
(intervals[i].end > p.end)p.end = intervals[i].end;
}
return
res;
}
};
|
本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3714681.html,如需转载请自行联系原作者