﻿ LeetCode Insert Interval-C 语言_软件编程-脚本宝典

# LeetCode Insert Interval

## LeetCode Insert Interval

LeetCode解题之Insert Interval原题给出多个不重合的数据区段，现在插入一个数据区段，有重合的区段要进行合并。注意点：所给的区段已经按照起始位置进行排序例子：输入: intervals = [2,

## AC源码

``````# Definition for an interval.
class Interval(object):
def __init__(self, s=0, e=0):
self.start = s
self.end = e

# To print the result
def __str__(self):
return "[" + str(self.start) + "," + str(self.end) + "]"

class Solution(object):
def insert(self, intervals, newInterval):
start, end = newInterval.start, newInterval.end
left = list(filter(lambda x: x.end < start, intervals))
right = list(filter(lambda x: x.start > end, intervals))
if len(left) + len(right) != len(intervals):
start = min(start, intervals[len(left)].start)
end = max(end, intervals[-len(right) - 1].end)

return left + [Interval(start, end)] + right

if __name__ == "__main__":
intervals = Solution().insert([Interval(2, 6), Interval(8, 10), Interval(15, 18)], Interval(13, 16))
for interval in intervals:
print(interval)``````

Tags：

﻿
<