题目描述:
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].
AC代码:
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
if not intervals:
return []
res = []
intervals.sort(key=lambda x: x.start)
temp = intervals[0]
for i in range(len(intervals) - 1):
if temp.end < intervals[i + 1].start:
res.append(temp)
temp = intervals[i + 1]
else:
temp.end = max(intervals[i + 1].end, temp.end)
res.append(temp)
return res