, insert and merge in as .
, insert and merge in as .
overlaps with .
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<Interval>::iterator itstart=intervals.begin(); for(; itstart!=intervals.end(); ++itstart)if((*itstart).start <= newInterval.start && newInterval.start <= (*itstart).end)break;vector<Interval>::iterator itend=intervals.begin();for(; itend!=intervals.end(); ++itend) if((*itend).start <= newInterval.end && newInterval.end <= (*itend).end)break;if(itstart!=intervals.end() && itend!=intervals.end()){if(itstart != itend){(*itstart).end = (*itend).end; int tmp = (*itstart).end; itstart++;while(itstart != intervals.end()) if((*itstart).start <= tmp) itstart = intervals.erase(itstart); else break;}return intervals;}else if(itstart==intervals.end() && itend!=intervals.end()){(*itend).start = newInterval.start; int tmp1 = (*itend).start; int tmp2 = (*itend).end; itstart = intervals.begin(); while((*itstart).start != tmp1 && (*itstart).end != tmp2) if((*itstart).start >= tmp1 && (*itstart).end <= tmp2) itstart = intervals.erase(itstart); else ++itstart;return intervals;}else if(itstart!=intervals.end() && itend==intervals.end()){(*itstart).end = newInterval.end; int tmp = newInterval.end; ++itstart;while(itstart != intervals.end()) if((*itstart).start < tmp) itstart = intervals.erase(itstart); else break;return intervals;}else{vector<Interval>::iterator it=intervals.begin();for(; it!=intervals.end(); ++it)if((*it).start > newInterval.start)break;//--it;vector<Interval>::iterator it1 = intervals.insert(it,newInterval); while(it1 != intervals.end()) { if((*it1).end < newInterval.end) it1 = intervals.erase(it1); else it1++; }return intervals;} }};