原题地址:https://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],[8,10],[15,18]
.
解题思路:先将区间按照每个start的值来排序,排好序以后判断一个区间的start值是否处在前一个区间中,如果在前一个区间中,那么合并;如果不在,就将新区间添加。
代码:
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = eclass Solution:
# @param intervals, a list of Interval
# @return a list of Interval
def merge(self, intervals):
intervals.sort(key = lambda x:x.start)
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
size=len(res)
if res[size-1].start<=intervals[i].start<=res[size-1].end:
res[size-1].end=max(intervals[i].end, res[size-1].end)
else:
res.append(intervals[i])
return res
[leetcode]Merge Intervals @ Python