[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘

56: Merge Intervalshttps://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].

===Comments by Dabay===这道题想起来比较简单,实现的时候恼火。技巧在与,不是一个个地插入到已有的序列,而是把已有的序列插入到一个标杆Interval前面、交叉、后面。

对于每一个序列中的Interval,    - 如果它的end比标杆的start小,插入到前面    - 如果它的start比标杆的end大,插入到后面    - 如果它和标杆有交集,更新标杆的start和end‘‘‘# Definition for an interval.class Interval:    def __init__(self, s=0, e=0):        self.start = s        self.end = e

class Solution:    # @param intervals, a list of Interval    # @return a list of Interval    def merge(self, intervals):        def insert(interval, merged):            new_merged = []            i = 0            s = interval.start            e = interval.end            while i < len(merged):                if merged[i].end < s:                    new_merged.append(merged[i])                    i = i + 1                    continue                if e < merged[i].start:                    new_merged = new_merged + [Interval(s, e)] + merged[i:]                    break                s = min(s, merged[i].start)                e = max(e, merged[i].end)                i = i + 1            else:                new_merged.append(Interval(s, e))            return new_merged

        if len(intervals) <= 1:            return intervals

        merged = [intervals[0]]        i = 1        while i < len(intervals):            merged = insert(intervals[i], merged)            i = i + 1

        return merged

def main():    sol = Solution()    i1 = Interval(2,3)    i2 = Interval(4,5)    i3 = Interval(2,2)    i4 = Interval(15,18)    intervals = [i1,i2,i3,i4]    intervals = sol.merge(intervals)    for interval in intervals:        print "[%s, %s] " % (interval.start, interval.end),    print

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2024-12-28 01:43:15

[Leetcode][Python]56: Merge Intervals的相关文章

【一天一道LeetCode】#56. Merge Intervals

一天一道LeetCode系列 (一)题目 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]. (二)解题 /* 本题的解题步骤: 1.对vector里面的每个Interval结构体按照start的值升序排列 2.遍历整个intervals,如果有重复区域就合并,如果没

【leetcode】56. Merge Intervals

@requires_authorization @create_time 2015.7.10 11:07 @author johnsondu @url https://leetcode.com/problems/merge-intervals/ /** * 进行数组排序,然后按照贪心策略进行合并,具体是考察 * 下一个元素的start是否是介于当前的start和end之间, * 如果是,则根据需要更新当前end. * 时间复杂度(O(nlogn))-->排序, 结果获取O(n) * 空间复杂度(

LeetCode --- 56. Merge Intervals

题目链接: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]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

56. Merge Intervals - LeetCode

Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并 Java实现: public List<Interval> merge(List<Interval> intervals) { if (intervals == null || intervals.size() == 0) return i

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, mer

No.56 Merge Intervals

No.56 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]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3

leetCode 56.Merge Intervals (合并区间) 解题思路和方法

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]. 思路:题意很明确,首先对各区间按开始来排序,最后遍历,如果前面和后面的区间有重合,合并. 具体代码: /** * Definition for an interval. * publi

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge 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. * struct Interval { * int start; * int end; * Interval() : start(0)

LeetCode 56. 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]. 题目标签:Array 这道题目给了我们一个区间的list,让我们返回一个list,是合并了所有有重叠的区间之后的list.这道题目的关键在于如何判断两个区间有重叠,根据原题给的例子可以看出,在按照每个区间的start排序