[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 ( ...
随机推荐
- bug 修改心得
我在做一个项目的时候,分页无法显示,于是我就开始进行各种修改. 最后我发现竟然是因为配置文件写错了,结果页面跳到别的页面, 跳转到了单项详细页面.
- HDU 5044 (树链剖分+树状数组+点/边改查)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5044 题目大意:修改链上点,修改链上的边.查询所有点,查询所有边. 解题思路: 2014上海网赛的变 ...
- 【wikioi】1282 约瑟夫问题
题目链接:http://www.wikioi.com/problem/1282/ 算法:线段树(名次树) 说明在代码里有了,直接上代码. #include <cstdio> using n ...
- 【NOI2015】荷马史诗
追逐影子的人,自己就是影子. ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛>和& ...
- sqlserver行列转换
sqlserver行转列 --创建行转列表及插入数据 create table tb_RowConvertToColumn ( username ) null, course ) null, scor ...
- python requests库学习
Python 第三方 http 库-Requests 学习 安装 Requests 1.通过pip安装 $ pip install requests 2.或者,下载代码后安装: $ git clone ...
- DWR入门教程
DWR(Direct Web Remoting)是一个WEB远程调用框架.利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给 ...
- 【转】【Asp.Net MVC】asp.net mvc Model验证总结及常用正则表达式
本文属转载,来源: http://www.byywee.com/page/M0/S868/868615.html 关于Model验证官方资料: http://msdn.microsoft.com/zh ...
- php 版本的indexof —— strpos坑爹
如果没有找到则会返回false,如果是在第一位,那么会返回0.那么在if判断的时候会非常麻烦. 我的方法是转化为string,当返回0的时候则是"0",如果返回的是false,则会 ...
- java 从args[]中赋值和程序直接赋值不一样?
System.out.println("对应的中文: " + decodeUnicode(args[0])); 赋值args[0] = “\u7b80\u4ecb” System. ...