My Python version


  • 0
    A
    length = len(intervals)
    if length == 0:
        return []
    # sorted by start
    intervals = sorted(intervals, key=lambda inter:inter.start)
    resList = []
    resList.append(Interval(intervals[0].start, intervals[0].end))
    length1 = 1
    for i in range(1, length):
        if intervals[i].start > resList[length1-1].end:
            resList.append(Interval(intervals[i].start, intervals[i].end))
                length1 += 1
        else:
            tmpInterval = Interval(min(resList[length1-1].start, intervals[i].start), max(resList[length1-1].end, intervals[i].end))
            resList[length1-1] = tmpInterval
            return resList 
    

Log in to reply
 

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