-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp056.cpp
54 lines (52 loc) · 1.31 KB
/
p056.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
43
44
45
46
47
48
49
50
51
52
53
54
/**
* 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(const Interval &i1, const Interval &i2)
{
if (i1.start != i2.start) return i1.start < i2.start;
return i1.end < i2.end;
}
class Solution {
public:
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> result;
unsigned len = intervals.size();
if (!len) return result;
Interval *a = new Interval[len];
int k;
for (k = 0; k < len; k++)
{
a[k] = intervals[k];
}
sort(a, a+len, cmp);
int start, end, i = 0, j;
while (i < len)
{
start = a[i].start;
end = a[i].end;
for (j = i+1; j < len; j++)
{
if (a[j].start <= end)
{
if (a[j].end > end)
{
end = a[j].end;
}
}
else
{
break;
}
}
result.push_back(Interval(start, end));
i = j;
}
return result;
}
};