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]
.
合并重叠区间
1 class Solution(object): 2 def merge(self, intervals): 3 """ 4 :type intervals: List[Interval] 5 :rtype: List[Interval] 6 """ 7 res = [] 8 for v in sorted(intervals,key=lambda x:x.start): 9 if res and v.start <= res[-1].end: 10 res[-1].end = max(res[-1].end,v.end) 11 else: 12 res.append(v) #or res += i, 13 return res