-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleet_56.cpp
38 lines (35 loc) · 1.01 KB
/
leet_56.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
static const auto speedup = [](){
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
static bool comp(const Interval & a, const Interval &b)
{
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> rc;
if (intervals.size() == 0)
return rc;
sort(intervals.begin(), intervals.end(), comp);
int curStart = intervals[0].start, curEnd = intervals[0].end;
for (unsigned i = 0; i < intervals.size(); i++)
{
if (curEnd >= intervals[i].start)
{
if (curEnd < intervals[i].end)
curEnd = intervals[i].end;
}
else
{
rc.push_back(Interval(curStart, curEnd));
curStart = intervals[i].start;
curEnd = intervals[i].end;
}
}
rc.push_back(Interval(curStart, curEnd));
return rc;
}
};