Given a collection of intervals, merge all overlapping intervals.
Examples
1 2 3 4
Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
1 2 3 4
Example 2: Input: [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping.
Solution
这道题和OJ上的看电视时间排序一题类似,以start递增排序,判断前一段区间的end是否大于后一段区间的start来进行合并。尽管AC了,但对象排序颇为耗时。看了解析,将start,end拆开放在两个数组。用一个索引记录区间。当 i <length - 1且 start[i+1] <= end[i].