-
Notifications
You must be signed in to change notification settings - Fork 1
/
MergeIntervals.cpp
44 lines (42 loc) · 1.01 KB
/
MergeIntervals.cpp
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
40
41
42
43
44
/*
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), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool compare(Interval a, Interval b)
{
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> res;
int len = intervals.size();
if(len<=1)
return intervals;
sort(intervals.begin(), intervals.end(), compare);
Interval Last = intervals[0];
for(int i=1;i<len;i++)
{
if(intervals[i].start<=Last.end)
Last.end = max(Last.end, intervals[i].end);
else
{
res.push_back(Last);
Last = intervals[i];
}
}
res.push_back(Last);
return res;
}
};