-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path056_MergeIntervals.cpp
48 lines (46 loc) · 1.38 KB
/
056_MergeIntervals.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
//90.65%
/**
* 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) {
int size(intervals.size());
if(size <= 1)
return intervals;
sort(intervals.begin(), intervals.end(), compareInterval);
vector<Interval> res;
for(int i = 1; i < size; i++){
if(intervals[i].start <= intervals[i - 1].end){
intervals[i].start = intervals[i - 1].start;
intervals[i].end = max(intervals[i - 1].end, intervals[i].end);
}else
res.push_back(intervals[i - 1]);
}
res.push_back(intervals[size - 1]);
return res;
}
private:
static bool compareInterval(Interval& v1, Interval& v2){
return v1.start < v2.start;
}
};
//In this way, we won't change the input reference parameter
vector<Interval> merge(vector<Interval>& ins) {
if (ins.empty()) return vector<Interval>{};
vector<Interval> res;
sort(ins.begin(), ins.end(), [](Interval a, Interval b){return a.start < b.start;});
res.push_back(ins[0]);
for (int i = 1; i < ins.size(); i++) {
if (res.back().end < ins[i].start) res.push_back(ins[i]);
else
res.back().end = max(res.back().end, ins[i].end);
}
return res;
}