057 Insert Intervals

题目: 057 Insert Intervals

这道题就是要考虑两个边界点是否会落到原有的intervals中的某一个区间之内 还是在外部, 分情况讨论即可

class Solution:
    # @param intervals, a list of Intervals
    # @param newInterval, a Interval
    # @return a list of Interval
    def insert(self, intervals, newInterval):
        intervals.insert(0, Interval(-1001,-1000))
        intervals.append(Interval(100000,100001))
        in_head = False
        in_tail = False
        i = 1
        start = newInterval.start
        end   = newInterval.end
        while i < len(intervals):
            if intervals[i-1].start <= start and start < intervals[i].start:
                head = i-1
                if start <= intervals[i-1].end:
                    in_head = True
            if intervals[i-1].end < end and end <= intervals[i].end:
                tail = i
                if end >= intervals[i].start:
                    in_tail = True
            i += 1
        if in_head and in_tail:
            intervals[tail].start = intervals[head].start
            intervals[head:tail] = []
        if in_head and not in_tail:
            intervals[head].end = end
            intervals[head+1:tail] = []
        if not in_head and in_tail:
            intervals[tail].start = start
            intervals[head+1:tail] = []
        if not in_head and not in_tail:
            intervals[head+1:tail] = []
            intervals.insert(head+1,Interval(start,end))
        return intervals[1:-1]
时间: 2024-12-29 11:44:10

057 Insert Intervals的相关文章

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Insert Intervals

Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]]. I

插入区间--Insert Intervals

https://leetcode.com/problems/insert-interval/ Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start t

Insert Interval &amp; Merge Intervals

Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert [2, 5] into [[1,2], [5,9]],

[LeetCode] 57. Insert Interval 解决思路

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [

【Leetcode】【Hard】Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [

2015.04.01 Leetcode Insert interval

Insert Interval解法: 先用start,end两个变量来定位 newinterval可能要插入的起始点和结束点. 分三种情况来看是否需要调整或合并(merge). case1: 在start 之前直接copy 到结果res: case2: 在end之后也是直接copy到res中; case3: 需要合并的就是newInterval.start, newInterval.end, 和任选的一个interval 的interval.start , interval.end 四个边界的比

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6]

[LeetCode] Insert Interval 二分搜索

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1:Given intervals [1,3],[6,9], insert and merge [