[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 ( ...
随机推荐
- 坑爹的strcat
strcat是会改变原来的字符型数组的值的. #include<stdio.h> #include<stdlib.h> #include<string.h> voi ...
- 20145315 《Java程序设计》实验三实验报告
实验三 敏捷开发与XP实践 实验内容 下载并学会使用git上传代码: 与同学结对,相互下载并更改对方代码,并上传: 实现代码的重载. 实验步骤 下载并用git上传代码: 1.下载并安装好git,在cm ...
- PNG无损压缩工具Optipng【备忘】
Optipng 是专门的 PNG 图像优化工具. 支持WINODWS.LINUX 地址:http://optipng.sourceforge.net/ 另:jpegoptim 优化 jpeg 图片 地 ...
- sql中的视图
视图可分为:普通视图和索引视图视图的作用: 1.提高数据库的安全性,降低数据库被攻击的风险,使外界无法得知数据库的数据结构 2.使查询更方便,简化程序员的操作非只读视图可以增删改,但不建议.视图的试用 ...
- Linux(Redhat)下redis安装
原文:http://www.javaweb1024.com/data/NoSQL/2015/06/29/785.html redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系 ...
- [FollowUp] Combinations 组合项
这是Combinations 组合项 的延伸,在这里,我们允许不同的顺序出现,那么新的题目要求如下: Given two integers n and k, return all possible c ...
- ArcEngine开发:IElement.Geometry 值不在预期范围内 + 元素绘制代码
IElement pEle = pLineEle as IElement; pEle.Geometry = pLn; pLn为一个ILine对象,想当然的以为它是IGeometry对象,可以赋值,结果 ...
- iOS 用宏定义写一个单例(Singleton)
用如下方法定义单例 @interface singleton_interface(ClassName); @end 实现单例在 @implemention singleton_implemention ...
- jQuery前端验证多种方式
JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...
- UE4.7的IOS发布和调试的相关问题
UE4.7以后正式源码免费了,正好最近工作也在做这部分,ue4的官方文档虽然有一部分ios平台的资料,那也只是通过编辑器来发布或预览一类,但手游程序员都知道,一些cpu和gpu性能上的调试是在所难免的 ...