[hard]57. Insert Interval
来源:互联网 发布:清除注册表的软件 编辑:程序博客网 时间:2024/05/29 13:53
57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.
This is because the new interval [4,9]
overlaps with [3,5],[6,7],[8,10]
.
Way to easy to be called hard.
class Solution {public: vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) { vector<Interval> ret; bool inserted = false; for (auto i: intervals) { if (i.end < newInterval.start) ret.push_back(i); else if (i.start > newInterval.end) { if (!inserted) ret.push_back(newInterval); ret.push_back(i); inserted = true; } else { if (i.end >= newInterval.start) newInterval.start = min(i.start, newInterval.start); if (i.start <= newInterval.end) newInterval.end = max(newInterval.end, i.end); } } if (!inserted) ret.push_back(newInterval); return ret; }};
0 0
- [hard]57. Insert Interval
- 57. Insert Interval Hard
- Hard-题目28:57. Insert Interval
- [Leetcode 57, Hard] Insert Interval
- Hard 57题 Insert Interval
- Leetcode 56. Merge Intervals && 57. Insert Interval(Hard)
- leetcode hard模式专杀之57. Insert Interval
- LeetCode OJ 57 Insert Interval [hard]
- LeetCode --- 57. Insert Interval
- [Leetcode] 57. Insert Interval
- [LeetCode]57.Insert Interval
- [leetcode] 57.Insert Interval
- 57. Insert Interval
- [leetcode] 57.Insert Interval
- 57. Insert Interval
- 57. Insert Interval
- lkeetcode 57. Insert Interval
- 57. Insert Interval
- 通用的增删改查方法(反射)附带MySQL数据库连接
- [openjudge] 数据包的调度机制(dp好题)
- NOIP 2004 虫食算
- windows2008 服务器基本设置
- mysql
- [hard]57. Insert Interval
- Python编码问题2
- 洛谷 P3386【模板】二分图匹配
- Java中的选择排序和冒泡排序的代码
- Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token
- 将(*)的ASCII码2AH送入内存段1000H开始的100个单元中
- intellij-idea中中编辑reactjs
- 关于 i++ 和 ++i 的区别
- 一天一条Linux指令-ps