-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeIntervals.h
43 lines (41 loc) · 951 Bytes
/
MergeIntervals.h
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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
/**
*time:O(nlogn) space:O(1)
*/
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.size()<2)return intervals;
sort(intervals.begin(),intervals.end(),Solution::cmp);
auto it=begin(intervals);
auto pre=it;
vector<Interval> res;
for(;it!=end(intervals);++it)
{
if(pre->end<it->start)
{
res.push_back(*pre);
pre=it;
}
else
{
if(pre->end<it->end)pre->end=it->end;
}
}
res.push_back(*pre);
return res;
}
private:
static bool cmp(Interval i1,Interval i2)
{
return i1.start<i2.start;
}
};