解题思路
插入数组并保持数组有序,然后使用第56题的方法合并
57. 插入区间
代码
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
bisect.insort_left(intervals, newInterval)
ans = []
for s, e in intervals:
if ans and s <= ans[-1][1]:
ans[-1][1] = max(ans[-1][1], e)
else:
ans.append([s, e])
return ans

效果图