# -*- 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. iOS opencv

    1.在iPhone上使用 OpenCV http://blog.csdn.net/kmyhy/article/details/7560472 2. OpenCV iOS Hello¶ http://d ...

  2. [置顶] vi、akw和sed总结

  3. python3-day2(基本回顾)

    1.作用域 1>外层变量可以被内层变更使用 2>内层变更不可以被外层使用 global nonlocal 2.对于Python,一切事物都是对象,对象基于类创建 3.练习 有如下值集合 [ ...

  4. Hibernate框架增删改查测试类归为一个类

    package cn.happy.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org ...

  5. 动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的

    本文转自:http://www.cnblogs.com/hnsdwhl/archive/2011/07/23/2114730.html 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态 ...

  6. Guice 学习(六)使用Provider注入服务( Provider Inject Service)

    1.定义接口 package com.guice.providerInject; import com.google.inject.ProvidedBy; public interface Servi ...

  7. 基于MapReduce的SimRank++算法研究与实现

    一.算法应用背景 计算广告学(Computational Advertising)是一门广告营销科学,以追求广告投放的收益最大化为目标,重点解决用户与广告匹配的相关性和广告的竞价模型问题,涉及到自然语 ...

  8. Ubuntu12.04 Git 服务器详细配置

    Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目,学过Linux的都知道,Git的优点我就不再多说了,我也是很喜欢Linux的.今天我们一起学习Git服务器在Ubunt ...

  9. RMAN-FORMAT-CONFIGURE及动态性能表

    一.FORMAT参数在备份过程中,可指定format参数来自定义备份片段的命令规则,比如: RMAN> BACKUP DATABASE FORMAT 'D:\BACKUP\%U'; RMAN&g ...

  10. [HeadFirst-HTMLCSS学习笔记][第十三章表格]

    表格 -table 块 tr 行 table row th 表头 table head td 表数据 table data; caption 表格标题 <table> <captio ...