/**
* 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> result;
if(intervals.size()==0){
result.push_back(newInterval);
return result;
}
Interval current;
for(int i=0;i<intervals.size();++i){
current=intervals[i];
if(newInterval.start>current.end)
result.push_back(current);
else if(newInterval.end<current.start){
result.push_back(newInterval);
while(i<intervals.size()){
result.push_back(intervals[i]);
++i;
}
} else{
newInterval.start=newInterval.start<current.start?newInterval.start:current.start;
newInterval.end=newInterval.end>current.end?newInterval.end:current.end;
}
}
if(result.size()==0||result.back().end<newInterval.start)
result.push_back(newInterval);
return result;
}
};
Leetcode: Insert Interval
最新推荐文章于 2021-01-09 13:08:34 发布