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].
题意:该题与56题非常相似,只是题意给出每一个区间的start是递增的。所以我们不须要排序。该题须要我们加入一个区间。然后进行融合。这题在56题的基础上添加了区间推断的复杂度。
包括例如以下如所看到的的六种情况。 而这六种情况又能够合并成三种解决方案。看例如以下代码:/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */public class Solution { public Listinsert(List intervals, Interval newInterval) { // 先推断newInterval是否在intervals的范围内 if (newInterval == null) return intervals; int len = intervals.size(); if (len == 0) { intervals.add(newInterval); return intervals; } List res=new ArrayList (); for(Interval interval:intervals) { if(interval.end newInterval.end)//newInterval插入最前端的情况 { res.add(newInterval); newInterval=interval;//这个地方非常重要。就是找到了待插入区间位置。指定新的newInterval,由于intervals中的区间也可能有相交的地方,须要融合。 }else if(interval.start<=newInterval.end||interval.end>=newInterval.start)//有重合部分的四种情况 { newInterval=new Interval(Math.min(interval.start,newInterval.start),Math.max(interval.end,newInterval.end)); } } res.add(newInterval); return res; }}