1 Star 0 Fork 0

MetaverseMobile/leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
57. 插入区间.cpp 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
TieNan2019 提交于 2021-02-24 10:39 . Create 57. 插入区间.cpp
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval)
{
vector<vector<int>> ans;
enum {
UNDONE,
MATCHING,
MATCHED
} state;
state = UNDONE;
int front = -1, back = -1;
for (int i = 0; i < intervals.size(); i++) {
if (state == UNDONE) {
if (newInterval.back() < intervals[i].front()) {
ans.push_back(newInterval);
ans.push_back(intervals[i]);
state = MATCHED;
}
else if (newInterval.front() > intervals[i].back()) {
ans.push_back(intervals[i]);
}
else {
front = min(newInterval.front(), intervals[i].front());
back = max(newInterval.back(), intervals[i].back());
state = MATCHING;
}
}
else if (state == MATCHED) {
ans.push_back(intervals[i]);
}
else {
if (back < intervals[i].front()) {
ans.push_back({front, back});
ans.push_back(intervals[i]);
state = MATCHED;
}
else {
front = min(front, intervals[i].front());
back = max(back, intervals[i].back());
}
}
}
if (state == MATCHING)
ans.push_back({front, back});
else if (state == UNDONE)
ans.push_back(newInterval);
return ans;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/MetaverseMobile/leetcode.git
[email protected]:MetaverseMobile/leetcode.git
MetaverseMobile
leetcode
leetcode
main

搜索帮助