题解 | #合并区间#

合并区间

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

!!!注意判空

!!!注意给定区间是否有序

# class Interval:
#     def __init__(self, a=0, b=0):
#         self.start = a
#         self.end = b

#
# 
# @param intervals Interval类一维数组 
# @return Interval类一维数组
#
class Solution:
    def merge(self , intervals ):
        # write code here
        if not intervals:
            return []

        intervals = sorted(intervals, key = lambda interval: interval.start)

        curr = intervals[0]

        res = []

        for i in range(1, len(intervals)):
            if curr.start <= intervals[i].start <= curr.end:
                curr.end = max(curr.end, intervals[i].end)
            else:
                res.append(curr)
                curr = intervals[i]

        res.append(curr)

        return res
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 13:15
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-18 13:28
已编辑
门头沟学院 Web前端
爱睡觉的冰箱哥:《给予你300的工资》,阴的没边了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务