forked from iphkwan/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_Intervals.cc
43 lines (43 loc) · 1.3 KB
/
Merge_Intervals.cc
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
/**
* 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(Interval A, Interval B) {
if (A.start != B.start) {
return A.start < B.start;
}
return A.end < B.end;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> ans;
int st_pos = 1, end_pos = 0;
for (int i = 0; i < intervals.size(); i++) {
if (st_pos > end_pos) {
st_pos = intervals[i].start;
end_pos = intervals[i].end;
} else {
if (intervals[i].start <= end_pos) {
end_pos = max(end_pos, intervals[i].end);
} else {
ans.push_back(Interval(st_pos, end_pos));
st_pos = intervals[i].start;
end_pos = intervals[i].end;
}
}
}
if (st_pos <= end_pos) {
ans.push_back(Interval(st_pos, end_pos));
}
return ans;
}
};