代码拉取完成,页面将自动刷新
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;
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。