1、问题
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2e9b78c6a0f26e0a04e4e8b0aedac51.png#pic_center)
2、算法标签
3、通过代码
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> ans;
int flag=1;
for(int i=0;i<intervals.size();){
if(newInterval[0]>intervals[i][1]){
ans.push_back(intervals[i++]);
}
else if(newInterval[0]<=intervals[i][1]){
int temp_l=min(newInterval[0],intervals[i][0]);
for(;i<intervals.size();i++){
if(newInterval[1]<intervals[i][0]){
ans.push_back({temp_l,newInterval[1]});
flag=0;
break;
}
else if(newInterval[1]<=intervals[i][1]){
ans.push_back({temp_l,intervals[i][1]});
i++;
flag=0;
break;
}
}
if(flag){
ans.push_back({temp_l,newInterval[1]});
flag=0;
break;
}
for(;i<intervals.size();i++){
ans.push_back(intervals[i]);
}
}
}
if(flag){
ans.push_back(newInterval);
}
return ans;
}
};
4、题目链接
LeetCode — — 插入区间