给出一个区间的集合,请合并所有重叠的区间。(解题思想来源于:https://blog.csdn.net/qq_34364995/article/details/80788049 )

示例 1:

输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

解题思路:

1.将intervals按每一个元素的start进行升序排列。
2.此时后一个值的start一定在前一个值的start后(或相等)。这个时候只要判断后一个的start是否比前一个的end大。这里我设置了两个指针l和h来表示区间的起始值和终点,列表res作为结果。判断:
如果 intervals[i].start <= intervals[i-1].end, 那么l保持不变,h为max(intervals[i].end, intervals[i-1].end)。否则,往列表res添加[l,h],更新l和h的值。接下来继续循环判断。
3.循环结束再往res添加[l,h]。
代码如下:

# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e class Solution:
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
if len(intervals) <= 1:
return intervals
res = []
intervals = sorted(intervals,key = lambda start: start.start)
l = intervals[0].start
h = intervals[0].end
for i in range(1,len(intervals)):
if intervals[i].start <= h:
h = max(h,intervals[i].end)
else:
res.append([l,h])
l = intervals[i].start
h = intervals[i].end
res.append([l,h])
return res

leetcode56:合并区间的更多相关文章

  1. [Swift]LeetCode56. 合并区间 | Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  2. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  3. lintcode:合并区间

    题目: 合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...

  4. LeetCode(56):合并区间

    Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...

  5. HRBUST - 1818 石子合并 区间dp入门

    有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...

  6. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  7. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  8. leetcode合并区间

    合并区间     给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...

  9. 合并区间(LintCode)

    合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 1 ...

随机推荐

  1. RFBnet论文笔记

    论文:Receptive Field Block Net for Accurate and Fast Object Detection 论文链接:https://arxiv.org/abs/1711. ...

  2. python基础之字典以及增删改查

    字典:字典是python中唯一的一个映射类型,主要形式为 dic = {key1:value,key2:value2,....} 字典中key的值是唯一的,主要关系到HASH算法,并且key的值必须是 ...

  3. Windows下如何使用Heroku

    1. 安装 进入https://devcenter.heroku.com/articles/heroku-cli#windows,选择对应版本安装 安装后使用heroku -v可检查版本号 2. 登陆 ...

  4. Bulb Switcher (leetcode java)

    问题描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  5. 00-自测4. Have Fun with Numbers

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  6. CSU OJ 1340 A Sample Problem

    Description My girlfriend loves 7 very much, she thinks it is lucky! If an integer contains one or m ...

  7. 安装sql 2008步骤以及所遇到的问题

    下载网址:http://www.xiazaiba.com/html/4610.html 安装步骤: 1.  在Windows7操作系统系,启动Microsoft SQL 2008安装程序后,系统兼容性 ...

  8. SpringBoot项目Shiro的实现(二)

    在看此小节前,您可能需要先看:http://www.cnblogs.com/conswin/p/7478557.html 紧接上一篇,在上一篇我们简单实现了一个Springboot的小程序,但我们发现 ...

  9. java中String创建对象分析(转)

    String str=new String("abc");   紧接着这段代码之后的往往是这个问题,那就是这行代码究竟创建了几个String对象呢? 相信大家对这道题并不陌生,答案 ...

  10. Vue引入jQuery

    1.在项目中安装jquery npm install jquery --save-dev 或者 打开package.json文件,在里面加入这行代码,jquery后面的是版本,根据你自己需求更改. d ...