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].

把一个区间插入一个已排序的无重叠的区间, 如果重叠的话,合并之

方法一:有点类似于快排的方法

 class Solution(object):
def insert(self, intervals, newInterval):
s,e = newInterval.start,newInterval.end
small = [ i for i in intervals if i.end<s ]
large = [ i for i in intervals if i.start>e ]
if small + large != intervals:
s = min(s,intervals[len(small)].start)
e = max(e,intervals[~len(large)].end)
return small+[Interval(s,e)]+large

方法二:和上面方法一样,不过这个代码太帅了!!!只需要遍历一次

 class Solution(object):
def insert(self, intervals, newInterval):
s,e = newInterval.start,newInterval.end
parts = mid,left,right=[],[],[]
for v in intervals:
parts[(v.end<s)-(v.start>e)].append(v)
if mid:
s = min(s,mid[0].start)
e = max(e,mid[-1].end)
return left+[Interval(s,e)]+right

[leetcode sort]57. Insert Interval的更多相关文章

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

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

  2. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  3. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

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

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

  5. [LeetCode] 57. Insert Interval 插入区间

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

  6. 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 ...

  7. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  8. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  9. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

随机推荐

  1. 微服务深入浅出(3)-- 服务的注册和发现Eureka

    现来说一些Eureka的概念: 1.服务注册 Register 就是Client向Server注册的时候提供自身元数据,比如IP和Port等信息. 2.服务续约 Renew Client默认每隔30s ...

  2. EOJ Monthly 2019.2 (based on February Selection) D.进制转换

    题目链接: https://acm.ecnu.edu.cn/contest/140/problem/D/ 题目: 思路: 我们知道一个数在某一个进制k下末尾零的个数x就是这个数整除kx,这题要求刚好末 ...

  3. Django连接mysql常见错误

    1045, "Access denied for user 'root'@'localhost' (using password: YES)" 数据库的密码或用户名不对,查看set ...

  4. 20165227 实验三《敏捷开发与XP实践》实验报告

    2017-2018-4 20165227 实验三<敏捷开发与XP实践>实验报告 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验要求 1.没有Linux基础的同学建议先学习&l ...

  5. equals方法变量和常量位置区别

    对于字符串比较,我的习惯用法是   变量.equals(常量) 比如:     a.equals("a") 今天看视频才知道变量在前面与后面有很大影响,正确的写法是常量放前面(可以 ...

  6. java浅复制与深使用接口实现

    1.浅复制与深复制概念⑴浅复制(浅克隆)被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象. ⑵深复 ...

  7. 【Educational Codeforces Round28】

    咸鱼选手发现自己很久不做cf了,晚节不保. A.Curriculum Vitae 枚举一下间断点的位置. #include<bits/stdc++.h> using namespace s ...

  8. iOS 里const在修饰对象时候的用法

    玩iOS的小伙伴对const应该很不陌生, 在声明全局常量的时候很多时候都会用到, 但是有时候修饰对象很迷惑下面是个人总结, 下面的地址都是模拟的 1. const NSString *str1 = ...

  9. Django集成Xadmin list index out of range报错解决方案

    return self.render(context) File "C:\Python36\lib\site-packages\django\template\defaulttags.py& ...

  10. http请求中的中文乱码问题

    通过浏览器访问服务器页面和资源时,不可避免地要传送中文字串,如果客户机与服务器不能用同一码表解析字串,肯定会出现各种各样的乱码问题.我总结了几个乱码场景及解决办法,如下 1.服务器上的中文字串被客户端 ...