[leetcode sort]57. 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].
把一个区间插入一个已排序的无重叠的区间, 如果重叠的话,合并之
方法一:有点类似于快排的方法
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的更多相关文章
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【一天一道LeetCode】#57. Insert Interval
一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 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 ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- LeetCode: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
随机推荐
- caffe设计网络教程(一)
假设现在我们要设计一个基于VGG的网络,主要考虑的问题是可否修改VGG类似于resnet那样,应该怎么修改?更具体来说,我们需要在VGG网络上考虑eltwise层,现在我们有三种方案,如下: 方案一: ...
- gnome桌面无法使用笔记本的触摸板
原来使用ubuntu的时候,升级了gnome之后触摸板就不能用了,不能说不能用了,应该是自己不会配置然后不好用了,具体状况如是,可以在登录界面gdm使用触摸板以及点击,但是进入桌面之后就不能点击了.后 ...
- java连接redis无法连接,报异常RedisConnectionException
不管是spring还是原生jedis连接redis,如果连不上多半是linux服务器的问题: 1 首先确保redis端口开放: 把6379或者redis的端口开放即可 2 redis.conf配置注释 ...
- collision
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAd0AAACYCAIAAAAuvaRSAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu
- javascript多投事件的处理 (转)
出处 http://blog.csdn.net/dead_of_winter/article/details/1646367 尽管ecma标准指定了addEventListener这样的方法来实现事件 ...
- 两个Bounding Box的IOU计算代码
Bounding Box的数据结构为(xmin,ymin,xmax,ymax) 输入:box1,box2 输出:IOU值 import numpy as np def iou(box1,box2): ...
- URIEncoding与useBodyEncodingForURI 在tomcat中文乱码处理上的区别
大家知道tomcat5.0开始,对网页的中文字符的post或者get,经常会出现乱码现象. 具体是因为Tomcat默认是按ISO-8859-1进行URL解码,ISO-8859-1并未包括中文字符,这样 ...
- MySQL 删除数据
删除数据的语句有三种:DELETE.DROP.TRUNCATE. 1.DELETE语句 DELETE 语句用于删除表中的行. 语法 DELETE FROM 表名称 WHERE 列名称 = 值 例如 - ...
- RabbitMQ--Publish/Subscribe(四)
先前例子中,我们创建了一个简单的日志系统,广播messages到consumer接收方. 但如果有日志错误级别的,不同的consumer接收不同错误级别的信息.比如consumer1接收info和wa ...
- Python_oldboy_自动化运维之路_函数,装饰器,模块,包(六)
本节内容 上节内容回顾(函数) 装饰器 模块 包 1.上节内容回顾(函数) 函数 1.为什么要用函数? 使用函数之模块化程序设计,定义一个函数就相当于定义了一个工具,需要用的话直接拿过来调用.不使用模 ...