-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Intervals
49 lines (42 loc) · 1.15 KB
/
Merge Intervals
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
45
46
47
48
49
/**
* 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 Compare
{
public:
bool operator () (const Interval& left, const Interval& right)
{
return left.start<right.start;
}
};
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(intervals.begin(), intervals.end(), Compare());
int len = intervals.size();
vector<Interval> res;
if (len <= 0)
return res;
res.push_back(intervals[0]);
for (int i=1; i<len; ++i)
{
if (intervals[i].start<=(*(res.end()-1)).end)
{
Interval tmpI = *(res.end()-1);
res.erase(res.end()-1);
res.push_back(Interval(tmpI.start, max(intervals[i].end, tmpI.end)));
}
else
res.push_back(intervals[i]);
}
return res;
}
};