My iterative AC Python solution


  • 0
    J
    def merge(self, intervals):
            if len(intervals)<=1:
                return intervals
            intervals=sorted(intervals,key=lambda x:x.start)
            i=0
            l=len(intervals)
            while i<l-1:
                if intervals[i].end<intervals[i+1].start:
                    i+=1
                    continue
                if intervals[i].end>intervals[i+1].end:
                    intervals.pop(i+1)
                    l-=1
                    continue
                if intervals[i].end>=intervals[i+1].start:
                    intervals[i].end=intervals[i+1].end
                    intervals.pop(i+1)
                    l-=1
            return  intervals

Log in to reply
 

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