57. Insert Interval
Solution (C++):
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> res;
int index = 0, m = intervals.size();
while (index < m && intervals[index].back() < newInterval.front()) {
res.push_back(intervals[index++]);
}
while (index < m && intervals[index].front() <= newInterval.back()) {
newInterval[0] = min(intervals[index][0], newInterval[0]);
newInterval[1] = max(intervals[index][1], newInterval[1]);
index++;
}
res.push_back(newInterval);
while (index < m) {
res.push_back(intervals[index++]);
}
return res;
}
相关链接如下:
知乎:littledy
GitHub主页:https://github.com/littledy
github.io:https://littledy.github.io/
欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可
![](https://www.cnblogs.com/images/cnblogs_com/dysjtu1995/1468066/t_qrcode_for_gh_547d3edeac81_258.jpg)
作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。