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. ETL testing

    https://www.tutorialspoint.com/etl_testing/index.htm querysurge-installer-6.0.5-linux-x64  测试ETL的工具.

  2. 矩阵 matrix

    传送门 注意这题时限是2s [问题描述] 有一个n × m的矩阵,你从左上角走到右下角,只能向下和向右走. 每个点上有一个重量v i,j 价值w i,j 的物品,你有一个容量为S的背包,经过一个点你可 ...

  3. Java编程思想 4th 第3章 操作符

    有了数据,还需要进行数据间的运算,因此Java中也有数据间运算的各种符号,书本称之为操作符,正确的翻译应该是运算符. Java中的运算符同C++相同,运算符同运算符对象构成表达式,表达式是运算对象及运 ...

  4. mysql取字段名注意事项!!!!千万不能和关键字同名

    今天就碰到一个恶心的问题,更新时update sql语句报错,查了半天感觉没问题啊,后来一行一行定位,终于找到原因了, 原来是有个字段是show,和mysql关键字冲突了,坑爹! 改了个名字就好了,或 ...

  5. 18 - csv文件-ini文件处理

    目录 1 CSV文件 1.1 手动生成一个csv文件 1.2 cvs模块 1.2.1 reader方法 1.2.2 writer方法 2 ini文件处理 2.1 configparser模块 2.2 ...

  6. MySQL5.7之多源复制&Nginx中间件(上)【转】

    有生之年系列----MySQL5.7之多源复制&Nginx中间件(上)-wangwenan6-ITPUB博客http://blog.itpub.net/29510932/viewspace-1 ...

  7. 十、springcloud之Consul注销实例

    @Autowired //com.ecwid.consul.v1.ConsulClient private ConsulClient consulClient; @PostMapping(" ...

  8. nodejs 接收上传的图片

    1.nodejs接收上传的图片主要是使用formidable模块,服务器是使用的express搭建. 引入formidable var formidable = require('./node_mod ...

  9. C# byte[] 转换16进制字符串

    1.byte[] 转换16进制字符串 1.1 BitConverter方式 var str = DateTime.Now.ToString(); var encode = Encoding.UTF8; ...

  10. mac搭建lamp环境

    转载:https://www.cnblogs.com/beautiful-code/p/7465320.html