forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.cpp
42 lines (40 loc) · 1021 Bytes
/
solution.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
/**
* 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 cmp(struct Interval in1,struct Interval in2)
{
return in1.start < in2.start;
}
class Solution
{
public:
vector<Interval> merge(vector<Interval> &intervals)
{
sort(intervals.begin(),intervals.end(),cmp);
vector<Interval> result;
if(intervals.size()==0)
return result;
int pend=intervals[0].end;
Interval s(intervals[0].start,pend);
for(int i=1;i<intervals.size();i++)
{
if(intervals[i].start>pend)
{
s.end=pend;
result.push_back(s);
s.start=intervals[i].start;
pend=intervals[i].end;
}
else pend=max(intervals[i].end,pend);
}
s.end=pend;
result.push_back(s);
return result;
}
};