56. Merge Intervals【H】【67】

来源:互联网 发布:linux查看系统字体设置 编辑:程序博客网 时间:2024/05/17 22:39

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].


Subscribe to see which companies asked this question


# Definition for an interval.# class Interval(object):#     def __init__(self, s=0, e=0):#         self.start = s#         self.end = eclass Solution(object):    def merge(self, intervals):        l = []        for i in intervals:            l += [i.start,i.end],                    l.sort()        res = []                i = 0        while i < (len(l)):        s = l[i][0]        e = l[i][1]                while i < len(l)-1 and l[i+1][0] <= e:        e = max(l[i+1][1],e)        i += 1        res += Interval(s,e),        i += 1                return res                                                                                """        :type intervals: List[Interval]        :rtype: List[Interval]        """


0 0