Thanks to help, my python code works.


  • 0
    J
    class Solution:
        # @param intervals, a list of Interval
        # @return a list of Interval
        def merge(self, intervals):
            if intervals == []:
                return []
            if len(intervals) <= 1:
                return [intervals[0]]
            intervals = sorted(intervals, key=lambda x:x.start)
            j = 0
            for i in intervals[1:]:
                if i.start <= intervals[j].end:
                    if i.end > intervals[j].end:
                        intervals[j].end = i.end
                    else:
                        intervals[j].end = intervals[j].end
                else: # i.start right at j.end
                    j += 1
                    intervals[j].start = i.start
                    intervals[j].end = i.end
                # print( [(k.start, k.end) for k in intervals])
                # print(j)
    
            for k in range(1, len(intervals)-j):
                intervals.pop()
            return intervals
    

    First time, final - pop() I set j+1 times, so it's wrong.
    After a graph of segments, it maybe clear. -- a suggestion hope for helpful


Log in to reply
 

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