Easy Python, first sort with start


  • 4
    W
    # 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]
            """
            intervals = sorted(intervals, key=lambda x: x.start)
            n=len(intervals)
            if n==0:
                return []
            re=[]
            re.append(intervals[0])
            for i in range(1,n):
                a=re[-1]
                b=intervals[i]
                if b.start>a.end:
                    re.append(b)
                else:
                    re[-1].end=max(a.end,b.end)
            return re

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.