839. Merge Two Sorted Interval Lists

Description

Merge two sorted (ascending) lists of interval and return it as a new sorted list. The new sorted list should be made by splicing together the intervals of the two lists and sorted in ascending order.

The intervals in the given list do not overlap.

The intervals in different lists may overlap.

Example

Example1

Input: list1 = [(1,2),(3,4)] and list2 = [(2,3),(5,6)]

Output: [(1,4),(5,6)]

Explanation:

(1,2),(2,3),(3,4) --> (1,4)

(5,6) --> (5,6)

Example2

Input: list1 = [(1,2),(3,4)] and list2 = [(4,5),(6,7)]

Output: [(1,2),(3,5),(6,7)]

Explanation:

(1,2) --> (1,2)

(3,4),(4,5) --> (3,5)

(6,7) --> (6,7)

思路

类似归并排序,只不过在排序过程中要进行区间合并,基本思路是记录合并后数组最后一个区间的end,每次加入新的区间就和end比较决定是否增加一个区间还是连接在原区间之后。

代码:

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • Introduction What is Bowtie 2? Bowtie 2 is an ultrafast a...
    wzz阅读 5,821评论 0 5
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,516评论 0 13
  • 今天2016年10月29日,我在花溪,7点钟的我刚从让人陶醉的小梦中醒来,肚子好难受啊,恩,喝一杯热水就好了,刚...
    茶与安生阅读 404评论 0 1
  • 2019届毕业典礼结束了,我头脑中每个孩子样子都还历历在目。 敏航--敢说真话,实事求是,喜欢你真实的样子。 智凯...
    rainbow琴阅读 563评论 0 0