# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 56: Merge Intervals
https://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)

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

  1. 【LeetCode】56. Merge Intervals

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

  2. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  4. LeetCode 题解 56. Merge Intervals

    题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...

  5. LeetCode OJ 56. Merge Intervals

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

  6. [leetcode sort]56. Merge Intervals

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

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

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

  8. 56. Merge Intervals - LeetCode

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

  9. 刷题56. Merge Intervals

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

随机推荐

  1. 漏洞:WebRTC 泄漏用户IP

    WebRTC又称为“网页即时通信”,是一组API函数,它经过W3C组织的认证,支持浏览器之间的语音通话.视频聊天和P2P模式分享文件.      这个协议主要包括:getUserMedia,RTCPe ...

  2. OpenStackCLI调试及术语识记

    1,Project are organizational units in the cloud,and are also known as tenants or accounts.Each user ...

  3. BackgroundWorker用法

    BackgroundWorker主要用来提供后台运算服务(防止用户前台无响应等待),并提供服务进度的类: 代码如下: BackgroundWorker bgw = new BackgroundWork ...

  4. pyqt labe界面超级链接例子学习

    def bz(self): self.lable1=QtGui.QLabel(u'<br><a href=http://windows.microsoft.com/zh-cn/win ...

  5. python标准库 platform模块

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #platform #作用:检查底层平台硬件,操作系统和解释器版本信 ...

  6. [AngualrJS] Using Angular-Cache for caching http request

    Check the website: https://jmdobry.github.io/angular-cache/#using-angular-cache-with-http Install: n ...

  7. 函数nvl,nvl2,nullif,coalesce

    NVL: Converts a null value to an actual valueNVL2:If expr1 is not null, NVL2 returns expr2. If expr1 ...

  8. [HeadFirst-HTMLCSS学习笔记][第八章扩大你的词汇量]

    字体 font-family,可指定多个候选 body{ font-family:Verdana,Geneva,Arial,sans-serif; } font-size 字体大小 body{ fon ...

  9. django中的Model模型一:

    在django的框架设计中采用了mtv模型,即Model,template,viewer Model相对于传统的三层或者mvc框架来说就相当对数据处理层,它主要负责与数据的交互,在使用django框架 ...

  10. ios开发 block语句块

    ios开发 block语句块 1.block 理解为匿名函数 2.block变量的定义 //定义block变量,^表示定义block //技巧:函数名左右加括号,在函数名前面在加^ void (^bl ...