forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.h
More file actions
26 lines (24 loc) · 722 Bytes
/
Copy pathsolution.h
File metadata and controls
26 lines (24 loc) · 722 Bytes
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
#include <vector>
using std::vector;
#include <algorithm>
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
std::sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){
return a.start < b.start;
});
for (const auto &interval : intervals) {
if (!ret.empty() && interval.start <= ret.back().end)
ret.back().end = std::max(interval.end, ret.back().end);
else ret.push_back(interval);
}
return ret;
}
};