题目来源


https://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].


题意分析


Input:a list of intervals

Output:a list of intervals but merged

Conditions:如果两个interval有重叠,则合并


题目思路


首先对所有的intervals按照start排序,然后如果后一个interval的start在前一个interval之中,那么合并这两个interval;如果不在,则把新的interval加入


AC代码(Python)


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

[LeetCode]题解(python):056-Merge Intervals的更多相关文章

  1. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

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

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

  3. Java for LeetCode 056 Merge Intervals

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

  4. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  5. LeetCode(56)Merge Intervals

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

  6. 【LeetCode】Merge Intervals 题解 利用Comparator进行排序

    题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...

  7. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  8. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. Merge Intervals - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...

  10. LeetCode: Merge Intervals 解题报告

    Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...

随机推荐

  1. BZOJ3738 : [Ontak2013]Kapitał

    $C_{N+M}^N=\frac{(N+M)!}{N!M!}$ 考虑求出$ans\bmod 10^9$的值 $10^9=2^9\times5^9$ 以$2^9$为例,先预处理出$1$..$2^9$中不 ...

  2. MathType 插入定义的chapter and section break后无法隐藏

    每一章标题后面插入一个“Next Section Break”,这样定稿后各章文件组合为总文件后,方程编号会自动递增,如果已经插入了默认的“Equation Chapter 1 Section 1”, ...

  3. [leetCode][012] Two Sum (1)

    [题目]: Given an array of integers, find two numbers such that they add up to a specific target number ...

  4. 【BZOJ】1441: Min(裴蜀定理)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1441 这东西竟然还有个名词叫裴蜀定理................ 裸题不说....<初等数 ...

  5. 【BZOJ】3289: Mato的文件管理(莫队算法+树状数组)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3289 很裸的莫队... 离线了区间然后分块排序后,询问时搞搞就行了. 本题中,如果知道$[l, r] ...

  6. eWebeditor编辑器上传图片路径错误解决方法[疑难杂症]【转,作者:unvs】

    做了一个多版本的网站,后台用的编辑器是eWebeditor,NET版,后面发现上传图片或者文件之后,路径错误无法显示,必须手工修改才行.. 为了更清楚的说明问题,我下面会说的比较详细,首先是网站文件框 ...

  7. 新浪微博iOS客户端架构与优化之路

    新浪微博iOS客户端架构与优化之路   随着Facebook.Twitter.微博的崛起,向UGC.PGC.OGC,自媒体提供平台的内 容消费型App逐渐形成了独特的客户端架构模式.与电商和通讯工具类 ...

  8. Qt Examples Qt实例汇总

    ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...

  9. [百科] - SIP(会话发起协议)

    SIP(会话发起协议)SIP是类似于HTTP的基于文本的协议.SIP可以减少应用特别是高级应用的开发时间.由于基于IP协议的SIP利用了IP网络,固定网运营商也会逐渐认识到SIP技术对于他们的深远意义 ...

  10. js - 犀牛学习

    1.id选择器 var ul = document.getElementById("empty"); 2.属性和事件配置 li.className = "ditem&qu ...