[LeetCode]题解(python):057-Insert Interval
题目来源
https://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].
题意分析
Input:a list of intervals and a new interval
Output:insert the new interval into the list and merge if necessary
Conditions:将新的interval插入进去,如果需要合并则合并
题目思路
感觉跟merge interval很像,所以直接append list中,然后排序按照上一题merge的方法,然后过了……
AC代码(Python)
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e class Solution(object):
def insert(self, intervals, newInterval):
"""
:type intervals: List[Interval]
:type newInterval: Interval
:rtype: List[Interval]
"""
intervals.append(newInterval)
intervals.sort(key = lambda x:x.start) length = len(intervals)
res = [] if length == 0:
res.append(newInterval)
return res res.append(intervals[0])
for i in range(1,length):
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]题解(python):057-Insert Interval的更多相关文章
- Java for LeetCode 057 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 057 Insert Interval 插入区间
给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...
- LeetCode(57) Insert Interval
题目 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals
Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【LeetCode】436. Find Right Interval 解题报告(Python)
[LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
随机推荐
- BZOJ4140 : 共点圆加强版
假设当前询问点为$(A,B)$,那么它在一个以$(x,y)$为圆心的圆里需要满足: $(x-A)^2+(y-B)^2\leq x^2+y^2$ $2Ax+2By\geq A^2+B^2$ 等价于询问所 ...
- BZOJ3743 : [Coci2014]Kamp
d[x][0]表示x点向下走且回到x点的最少代价 d[x][1]表示x点向下走但不回到x点的最少代价 d[x][2]表示x点向下走的最长路 d[x][3]表示x点向下走的次长路 u[x][0]表示x点 ...
- HDU 2653 (记忆化BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...
- 生成CSV文件后再将CSV文件导入到mysql
1.生成CSV jar包:http://pan.baidu.com/s/1xIL26 String csvFilePath = "d:\\test.csv"; CsvWriter ...
- ios CGImageRelease 出现 EXC_BAD_ACCESS的错误 ios特定形状剪裁图片 内存泄露
CGImageRef imgRef = [image CGImage]; 通过此种方式的得到的CGImageRef不能利用CGImageRelease释放,因为你不拥有它所以不用释放 在ios中特定形 ...
- HDU 4669 Mutiples on a circle(环状DP)
题目链接 这是最早看懂题意的一题,状态转移,挺好想..但是比赛时候,就是没有想到怎么去重,而且当时有些情况,也没注意到. 先预处理的dp[0]的情况,就是以p[0]为结尾的情况.之后D就行了,例如样例 ...
- Linux启动过程中几个重要配置文件的执行过程
Linux 登录后,配置执行顺序为(Debian Serials Capable):/etc/environment -> /etc/profile -> (~/.bash_profile ...
- Shell下的正则表达式 (鸟哥私房菜)
"Open Source" is a good mechanism to develop programs.$ apple is my favorite food.$ Footba ...
- System-Defined Device Setup Classes Available to Vendors
https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx
- 用Editplus开发HTML
准备工作 设置Editplus默认的打开浏览器为系统默认浏览器 取消Editplus的临时缓存文件 ☆ 设置不生成临时文件 这里的临时文件,指的是.bak文件,当你在Editplus下修改任意一个文件 ...