-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeIntervals.cpp
More file actions
45 lines (39 loc) · 966 Bytes
/
MergeIntervals.cpp
File metadata and controls
45 lines (39 loc) · 966 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Given a collection of intervals, merge all overlapping intervals.
//
//For example,
//Given[1, 3], [2, 6], [8, 10], [15, 18],
//return[1, 6], [8, 10], [15, 18].
//
#include<vector>
#include<algorithm>
using namespace std;
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
static bool comp(Interval a, Interval b) // 注意comp函数可以为static bool或者bool,返回值为bool类型。a.start<b.start为升序
{
return a.start < b.start;
}
class MergeIntervals {
public:
vector<Interval> merge(vector<Interval>& intervals) {
int n = intervals.size();
vector<Interval> res;
if (n == 0) return res;
sort(intervals.begin(), intervals.end(), comp);
res.push_back(intervals[0]);
for (int i = 1; i<n; ++i){
Interval cur = intervals[i];
if (cur.start>res.back().end){
res.push_back(cur);
}
else{
res.back().end = max(res.back().end, cur.end);
}
}
return res;
}
};