题目来源
https://leetcode.com/problems/insert-interval/description/
题目描述
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:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Example 2:
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].
给出一个二元组构成的数组. 每个二元组表示一个区间, 这些在数组中的区间事先已经排好序并保证了没有重叠. 现在的要求是在其中插入一个新的区间, 仍然要求新的区间插入后, 数组也应当满足严格升序并且区间之间没有重叠.
解题思路
上手题目之前我一直在担心TLE的问题, 试图用二分查找来解决问题. 但试着打了几行代码之后便发现, 用二分的话逻辑实在太复杂, 要考虑的情形太多. 这时候瞄了一眼题目的讨论区, 发现因为数组的插入和删除操作是