-
Notifications
You must be signed in to change notification settings - Fork 0
/
_057.cpp
36 lines (36 loc) · 1.06 KB
/
_057.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
/**
* 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> ret;bool flag = false;
for(int i = 0;i < intervals.size();i++){
if(flag) ret.push_back(intervals[i]);
else if(newInterval.start <= intervals[i].end){
if(newInterval.end < intervals[i].start){
ret.push_back(newInterval);
flag = true;
i--;
}
else{
newInterval.start = min(newInterval.start,intervals[i].start);
newInterval.end = max(newInterval.end,intervals[i].end);
}
}
else{
ret.push_back(intervals[i]);
}
}
if(!flag){
ret.push_back(newInterval);
}
return ret;
}
};