Given a collection of intervals, merge all overlapping intervals.For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].数组
时间 O(NlogN) 空间 O(N)app
首先根据Interval的起点,咱们将其排序,这样能合并的Interval就必定是相邻的了:code
[1,3] [5,6] [2,3] ---> [1,3] [2,3] [5,6]
而后咱们就顺序遍历这个列表,并记录一个当前待合并的Interval,若是遍历到的Interval和当前待合并的Interval有重复部分,咱们就将两个合并,若是没有重复部分,就将待合并的Interval加入结果中,并用新的Interval更新这个待合并的Interval。由于数组已经排过序,前面的Interval的起点确定小于后面Interval的起点,因此在判断是否有重叠部分时,只要考虑待合并的Interval的终点和遍历到的Interval的起点的关系就好了。排序
public class Solution { public List<Interval> merge(List<Interval> intervals) { List<Interval> res = new LinkedList<Interval>(); if(intervals.size() == 0){ return res; } // 按照起点排序 Collections.sort(intervals, new Comparator<Interval>(){ public int compare(Interval i1, Interval i2){ return i1.start - i2.start; } }); // 拿出第一个待合并的Interval Interval curr = intervals.get(0); for(Interval itv : intervals){ // 若是有重叠部分,更新待合并的Interval的起点和终点 if(curr.end >= itv.start){ curr.start = Math.min(curr.start, itv.start); curr.end = Math.max(curr.end, itv.end); } else { // 不然将待合并的Interval加入结果中,并选取新的待合并Interval res.add(curr); curr = itv; } } // 将最后一个待合并的加进结果 res.add(curr); return res; } }
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.leetcode
Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].get
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].it
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].io
时间 O(NlogN) 空间 O(N)class
和Merge Interval的思路接近,这题中咱们只有一个待合并的Interval,就是输入给出的。咱们只要把全部和该Interval有重叠的合并到一块儿就好了。为了方便操做,对于那些没有重叠部分的Interval,咱们能够把在待合并Interval以前的Interval加入一个列表中,把以后的Interval加入另外一个列表中。最后把前半部分的列表,合并后的大Interval和后半部分的列表连起来,就是结果了。List
public class Solution { public List<Interval> insert(List<Interval> intervals, Interval newInterval) { List<Interval> before = new LinkedList<Interval>(); List<Interval> after = new LinkedList<Interval>(); List<Interval> res = new LinkedList<Interval>(); if(intervals.size() == 0){ res.add(newInterval); return res; } // 排序 Collections.sort(intervals, new Comparator<Interval>(){ public int compare(Interval i1, Interval i2){ return i1.start - i2.start; } }); for(Interval itx : intervals){ // 将待合并Interval以前的存入一个列表中 if(newInterval.start > itx.end){ before.add(itx); } // 将有重叠的Interval都合并起来 if((newInterval.end >= itx.start && newInterval.end <= itx.end) || (newInterval.start <= itx.end && newInterval.start >= itx.start)){ newInterval.start = Math.min(newInterval.start, itx.start); newInterval.end = Math.max(newInterval.end, itx.end); } // 将待合并Interval以后的存入一个列表中 if(newInterval.end < itx.start){ after.add(itx); } } // 把三部分加起来 res.addAll(before); res.add(newInterval); res.addAll(after); return res; } }