原题地址:https://oj.leetcode.com/problems/insert-interval/

题意:

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

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

解题思路:最简单的方法是将要插入的区间和原来的区间合在一起排序,然后按照merge intervals的方法来编程。

代码:

# 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 Intervals
# @param newInterval, a Interval
# @return a list of Interval
# @should rewrite without sort!!!
def insert(self, intervals, newInterval):
intervals.append(newInterval)
intervals.sort(key = lambda x:x.start)
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
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]Insert Interval @ Python的更多相关文章

  1. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  2. [LeetCode] Insert Interval 插入区间

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

  3. Leetcode Insert Interval

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

  4. 57[LeetCode] Insert Interval

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

  5. [LeetCode] Insert Interval 二分搜索

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

  6. [LeetCode]Insert Interval 考虑多种情况

    写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)|  (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...

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

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

  8. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  9. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

随机推荐

  1. Android动态获取权限

    android权限的变化 在Android6.0以前的版本的时候,Android的权限都是在安装的时候全部的配置完成的.然而这往往会造成一些安全的问题. Google的解决办法: 将Android中的 ...

  2. zoj 3460 二分+二分图匹配

    不错的思想 /* 大致题意: 用n个导弹发射塔攻击m个目标.每个发射架在某个时刻只能为 一颗导弹服务,发射一颗导弹需要准备t1的时间,一颗导弹从发 射到击中目标的时间与目标到发射架的距离有关.每颗导弹 ...

  3. AtomicReference,AtomicStampedReference与AtomicMarkableReference的区别

    AtomicReference 通过volatile和Unsafe提供的CAS函数实现原子操作. 自旋+CAS的无锁操作保证共享变量的线程安全 value是volatile类型,这保证了:当某线程修改 ...

  4. spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法

    turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...

  5. spring-boot 速成(4) 自定义配置

    spring-boot 提供了很多默认的配置项,但是开发过程中,总会有一些业务自己的配置项,下面示例了,如何添加一个自定义的配置: 一.写一个自定义配置的类 package com.example.c ...

  6. STM32 System and Timer Clock Configurations

    STM32 System and Timer Clock Configurations I've started writing some software to drive a series of  ...

  7. Serial Wire Debug (SWD) Interface -- PSoc5

    PSoC 5 supports programming through the serial wire debug (SWD) interface. There are two signals in ...

  8. 在Visual Studio中使用组件图描述项目组件依赖关系

    如果想描述项目组件的关系,可以考虑使用UML组建图. 在建模项目下添加一个名称为"Applicaiton Component Structure"的UML组建图. 添加各个组件,并 ...

  9. Android如何运行真机在eclipse上调试应用?

    主要通过以下几个步骤: 1.手机通过数据线连接在电脑上 2.设置android手机为USB调试模式.步骤: menu—> 设置 —> 应用程序 —> 开发 , 选择[USB调试] 3 ...

  10. ios6sdk 和ios7sdk 分别在ios6设备和ios7设备上的效果 对比