[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 ( ...
随机推荐
- System call in linux by C
1: #include <stdlib.h> 2: int system(const char *command); 3: 4: while (something) { 5: int r ...
- 移动前端调试方案(Android + Chrome 实现远程调试)
一:背景 通常情况我们调试移动端页面最常用的方法就是:切换pc端浏览器的userAgent来模拟手机或其他移动设备调试页面 然后用手机打开要调试的页面 刷新页面查看调试结果 但是这就存在两个问题 在p ...
- Quartz Cron 表达式
Cron 表达式包括以下 7 个字段 格式: [秒] [分] [小时] [日] [月] [周] [年] 说明 是否必填 允许填写的值 允许的通配符 秒 是 - , - * / 分 是 - , - * ...
- JavaScript基础知识总结
正则表达式: 是一种专门用于操作字符串规则. 正则表达式: 通过一些符号来表达,简化对字符串的复杂操作. 弊端:阅读性较差 常见操作: 1.匹配 String matches(regex) 2.获取( ...
- shell运算
- 关于iOS手势
引: 前几天遇到一个坑,又仔细分析了一下事件的原理,不得不承认苹果的文档还是写的挺好的,网上就搜不到有几篇博客是介绍这个的,都是一些关于基本的用法的.这里纪录一下. 1.关于事件响应链. a.硬件接收 ...
- OpenCV 3.0 VS2010 Configuration
Add in the system Path: C:\opencv\build\x86\vc10\bin; Project->Project Property->Configuration ...
- [百科] - iLBC
iLBC是一种专为包交换网络通信设计的编解码,优于目前流行的G.729.G.723.1,对丢包进行了特有处理,即使在丢包率相当高的网络环境下,仍可获得非常清晰的语音效果. 30ms ptime的iLB ...
- lucene 3.0.2 基本操作入门
转自:Bannings http://blog.csdn.net/zhangao0086/article/details/ 我们为什么需要Lucene? 任何的的查询功能都类似,都是对文本内容的搜索, ...
- [转]Web Service Authentication
本文转自:http://www.codeproject.com/Articles/9348/Web-Service-Authentication Download source files - 45. ...