-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Intervals.txt
32 lines (32 loc) · 937 Bytes
/
Merge Intervals.txt
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
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
struct myclass{
bool operator()(const Interval& a, const Interval& b) {
return a.start < b.start;
}
} myobj;
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(), myobj);
vector<Interval> ret;
while (!intervals.empty()) {
vector<Interval>::iterator it = intervals.begin();
if (ret.empty() || ((*it).start > ret.back().end))
ret.push_back(*it);
else
ret.back().end = max(ret.back().end, (*it).end);
intervals.erase(it);
}
return ret;
}
};