题解 | #合并区间#

合并区间

http://www.nowcoder.com/practice/69f4e5b7ad284a478777cb2a17fb5e6a

Python版本,仅供参考

 class Interval:
    def __init__(self, a=0, b=0):
         self.start = a
         self.end = b
class Solution:
    def merge(self , intervals ):
        intervals = sorted(intervals,key=lambda x:x.start)
        size = len(intervals)
        i = 0;
        while i < size-1:
            if intervals[i+1].start <= intervals[i].end and intervals[i+1].end >= intervals[i].end:
                intervals[i+1].start = intervals[i].start;
                intervals.pop(i);
            elif intervals[i+1].start >= intervals[i].start and intervals[i+1].end <= intervals[i].end:
                intervals.pop(i+1);
            else:
                i+=1
            size = len(intervals)
        return intervals
全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
1
分享
牛客网
牛客企业服务