-
Notifications
You must be signed in to change notification settings - Fork 213
/
MergeIntervals.h
52 lines (49 loc) · 1.32 KB
/
MergeIntervals.h
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
/*
Author: Annie Kim, [email protected]
Date: Jun 7, 2013
Update: Jul 15, 2013
Problem: Merge Intervals
Difficulty: Medium
Source: http://leetcode.com/onlinejudge#question_56
Notes:
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].
Solution: 1. Sort in ascending order of 'start'.
2. Traverse the 'intervals', merge or push...
*/
/**
* 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 compare(Interval a, Interval b)
{
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
int N = intervals.size();
if (N <= 1) return intervals;
sort(intervals.begin(), intervals.end(), mycompare);
vector<Interval> res;
Interval last = intervals[0];
for (int i = 1; i < N; ++i)
{
if (intervals[i].start > last.end) {
res.push_back(last);
last = intervals[i];
} else {
last.end = max(last.end, intervals[i].end);
}
}
res.push_back(last);
return res;
}
};