merge intervals
Given a collection of intervals, merge all overlapping intervals.
先排序,之后覆盖,使用key对列表的元素排序
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
res = []
for i in sorted(intervals, key = lambda p: p.start):
if res and i.start <= res[-1].end:
res[-1].end = max(res[-1].end, i.end)
else :
res += [i]
return res