问题是:Given a collection of intervals, merge all overlapping intervals.
For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].
我尝试了我的解决方案:# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution:
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
start = sorted([x.start for x in intervals])
end = sorted([x.end for x in intervals])
merged = []
j = 0
new_start = 0
for i in range(len(start)):
if start[i]
continue
else:
j = j + 1
merged.append([start[new_start], end[j]])
new_start = i
return merged
但是,它显然缺少最后一个间隔,因为:
^{pr2}$
不确定如何包括最后的间隔,因为重叠只能在前进模式下检查。在
如何修正我的算法,使其工作到最后一个时隙?在
谢谢你的帮助。在