56. Merge Interval

0. 参考文献

序号 文献
1 花花酱 LeetCode 56. Merge Intervals
2 [LeetCode] Merge Intervals 合并区间

1. 题目

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, merge them into [1,6].

Example 2:

Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

2. 思路

本题是对区间的合并。首先需要对输入的区间按第一个数字进行排序。然后把数组的第一个区间放入结果集中,后续的区间依次和其比较。如果能合并就合并,不能的话就把当前区间放入结果集。

3. 实现

class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
ret = []
if len(intervals) == 0 :return ret
ret = []
intervals.sort(key = lambda x:x[0]) ret.append(intervals[0])
for i in range(1,len(intervals)):
if intervals[i][0] >= ret[-1][0] and intervals[i][0] <= ret[-1][1] :
t = [ret[-1][0],max(intervals[i][1],ret[-1][1])]
ret[-1] = t
else:
ret.append(intervals[i])
return ret

56. Merge Interval的更多相关文章

  1. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  2. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  3. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  4. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

  5. LintCode 156: Merge Interval

    LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...

  6. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  7. 刷题56. Merge Intervals

    一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...

  8. 56. Merge Intervals 57. Insert Interval *HARD*

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

  9. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

随机推荐

  1. ef core code first from exist db

    目标 为现有数据库生成新的连接,允许只选择部分表 可以处理一些很怪的需求,比如EF升级EF Core(这个可能有其他解),EF.EF Core同时连接一个数据库 我遇到的问题是: 原项目是.net f ...

  2. Java之线程池管理

    JDK5后建议使用ExecutorService与Excutors来创建与管理线程池, 不再建议直接使用Thread. 开始不明白原因, 今天知道结果了:使用Thread.currnetThread. ...

  3. 【转】python Counter模块

    >>> c = Counter() # 创建一个新的空counter >>> c = Counter('abcasdf') # 一个迭代对象生成的counter & ...

  4. ASP.NET WebForm项目--页面正确返回404及500等状态

    实现方法 项目路径 <system.webServer> <httpErrors errorMode="Custom" > <remove statu ...

  5. 淘宝开源Key/Value结构数据存储系统Tair技术剖析

    摘要: Tair的功能 Tair是一个Key/Value结构数据的解决方案,它默认支持基于内存和文件的两种存储方式,分别和我们通常所说的缓存和持久化存储对应. Tair除了普通Key/Value系统提 ...

  6. new和delete必须成对出现吗?【网上集合贴+个人总结】

    new和delete必须成对出现吗?[网上集合贴+个人总结] 1.从内存泄露与否的角度考虑 new 和 delete不一定要成对出現.理论上是這樣的.但是从习惯上來說,new delete成對出現是一 ...

  7. 值得推荐的C/C++框架和库(深度好文)

    [本文系外部转贴,原文地址:http://www.cppblog.com/merlinfang/archive/2014/12/26/209311.html http://coolshell.info ...

  8. Qt云服务/云计算平台QTC(Qt Cloud Services)入门(0)

    在这个“大数据”的时代,传统的跨平台C++库Qt已经将魔爪丧心病狂的伸向了“云计算”.在2012年的Qt开发者大会上,Qt发布了BaaS(Backend as a Service)服务——Engini ...

  9. C#调用记事本并填写内容

    using System.Runtime.InteropServices; using System.Diagnostics;   [DllImport("User32.DLL") ...

  10. Qt 设置背景图片3种方法(三种方法:QPalette调色板,paintEvent,QSS)

    方法1. setStylSheet{"QDialog{background-image:url()"}}  //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...