虽然是hard,但是意外的不是很难和56题相类似
有一堆区间,这些区间相互没有交集,且顺序按照start从小到大排列,现在插入一个新的区间,求合并后的区间列表
将原来的区间分为三种,一种在插入区间之前的,一种和插入区间有交集的,一种在插入区间之后的,分别处理即可
class Solution: def insert(self, intervals, newInterval): """ :type intervals: List[Interval] :type newInterval: Interval :rtype: List[Interval] """ result = [] if not intervals: return [newInterval] intervals_len = len(intervals) i = 0 while(i<intervals_len and intervals[i].end < newInterval.start): result.append(intervals[i]) i += 1 while(i<intervals_len and intervals[i].end >= newInterval.start and intervals[i].start <= newInterval.end): newInterval.start = min(intervals[i].start, newInterval.start) newInterval.end = max(intervals[i].end, newInterval.end) i += 1 result.append(newInterval) while(i<intervals_len): result.append(intervals[i]) i += 1 return result