[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 ...
 
随机推荐
- Vue 表格内容根据后台返回状态位填充文字
			
本文地址:http://www.cnblogs.com/veinyin/p/8534365.html Vue 做表格时我们常用的就是 v-for ,直接把 prop 绑定上去,但是如果表格内容需要我 ...
 - Linux基础-free窥内存-dd探硬盘
			
监控内存篇(RAM)-free free指令可以很直观的看到内存的使用情况 free -m指令以单位为MB的方式查看内存的使用情况(free命令读取的文件是/proc/meminfo) 这个表格的解释 ...
 - [转]坐在马桶上看算法:只有五行的Floyd最短路算法
			
此算法由Robert W. Floyd(罗伯特·弗洛伊德)于1962年发表在“Communications of the ACM”上.同年Stephen Warshall(史蒂芬·沃舍尔)也独立发表了 ...
 - 【navicat112_premium】navicat112_premium数据库连接工具安装过程
			
此工具及其方便,可以连接mysql.oracle.sqlserver登数据库... 1.下载安装包Navicat Premium_11.2.7简体中文版.rar 下载地址:http://qiaoliq ...
 - 【codeforces】【比赛题解】#920 Educational CF Round 37
			
[A]浇花 题意: 一个线段上每个整点都有花,有的点有自动浇花的喷水器,有问几秒能浇完所有的花. 题解: 大模拟 #include<cstdio> #include<cstring& ...
 - Framebuffer 驱动学习总结(一) ---- 总体架构及关键结构体
			
一.Framebuffer 设备驱动总体架构 帧缓冲设备为标准的字符型设备,在Linux中主设备号29,定义在/include/linux/major.h中的FB_MAJOR,次设备号定义帧缓冲的个数 ...
 - textarea保留换行和空格
			
<style> pre {white-space: pre-wrap;} </style> //替换textare <pre class="feedback_q ...
 - p,br,hn,b,i,u,s,sup,sub标签
			
<!-- -->注释 <p></p>段落标签 <br />换行标签 <h1></h1> 字体标签 最大 <h6> ...
 - 博客转移至github
			
博客转移到github 鉴于github的各种优势,博客转移!
 - 安装pywin32模块
			
1.先下载pywin32对于的版本 下载地址:python for windows extensions 2.选择自己对应的版本,我的是python3.5版本 注意注意注意:此处一定要看清楚自己的py ...