Python Solution one pass


  • 0
    class Solution(object):
        def insert(self, intervals, newInterval):
            i = 0
            while i <= len(intervals):
                if i == len(intervals) or intervals[i].start > newInterval.end:
                    intervals.insert(i,newInterval)
                    break
                if intervals[i].end < newInterval.start:
                    i += 1
                    continue
                newInterval.start, newInterval.end = min(newInterval.start, intervals[i].start), max(newInterval.end, intervals[i].end)
                intervals.pop(i)
            return intervals

Log in to reply
 

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