-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInsert_Intervals.cpp
38 lines (37 loc) · 1.05 KB
/
Insert_Intervals.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
// http://oj.leetcode.com/problems/insert-interval/
/**
* 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> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval>::iterator it = intervals.begin();
while (it != intervals.end())
{
if (newInterval.end < it->start)
{
intervals.insert(it, newInterval);
return intervals;
}
else if (newInterval.start > it->end)
{
it++;
continue;
}
else
{
newInterval.start = min(newInterval.start, it->start);
newInterval.end = max(newInterval.end, it->end);
it = intervals.erase(it);
}
}
intervals.insert(intervals.end(), newInterval);
return intervals;
}
};