-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path56_gp.cpp
35 lines (32 loc) · 1.01 KB
/
56_gp.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
/**
* 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 Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
if(intervals.size() == 0)
return result;
sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b){return a.start < b.start;});
result.push_back(intervals[0]);
for (int i = 1; i < intervals.size(); ++i) {
Interval last = result.back();
if (intervals[i].start > last.end) {
result.push_back(intervals[i]);
}else{
Interval tmp;
tmp.start = last.start;
tmp.end = max(last.end, intervals[i].end);
result.pop_back();
result.push_back(tmp);
}
}
return result;
}
};