题目来源


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的更多相关文章

  1. Java for LeetCode 057 Insert Interval

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  2. 057 Insert Interval 插入区间

    给出一个无重叠的按照区间起始端点排序的区间列表.在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间).示例 1:给定区间 [1,3],[6,9],插入并合并 ...

  3. LeetCode(57) Insert Interval

    题目 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece ...

  4. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  5. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  6. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

  7. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  8. 【LeetCode】436. Find Right Interval 解题报告(Python)

    [LeetCode]436. Find Right Interval 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  10. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

随机推荐

  1. BZOJ1769 : [Ceoi2009]tri

    将所有点极角排序,建立线段树,线段树每个节点维护该区间内所有点组成的上下凸壳. 对于一个查询,二分查找出相应区间的左右端点,在线段树上得到$O(\log n)$个节点,在相应凸壳上三分查找出与斜边叉积 ...

  2. Photoshop: 机关单位公章

    机关单位公章的大小与机构的级别有关,级别越高的公章越大,一般直径在3.8-4.2cm,很少有用4.5cm或3.4cm的.但企业的公章一般都很大. 首先点击文件新建,新建一个500×500像素(像素大小 ...

  3. 转载 模板整理 by gc812

    http://www.cnblogs.com/gc812/p/5779789.html 上友链,不盗版 CC BY-NC-SA

  4. BZOJ4408: [Fj Winter Camp 2016]神秘数

    Description 一个可重复数字集合S的神秘数定义为最小的不能被S的子集的和表示的正整数.例如S={1,1,1,4,13}, 1 = 1 2 = 1+1 3 = 1+1+1 4 = 4 5 = ...

  5. git 基于发布分支的开发

    创建发布分支: (1) 软件hello-world的1.0发布版本库中有一个里程相对应. /home/jackluo/workspace/user1/workspace/hello-worldgit ...

  6. java+easyui实例

    1.首先引入easyui包 在jsp页面上引用以下文件: <link rel="stylesheet" type="text/css" href=&quo ...

  7. FLTK 1.3.3 VS 2010 Configuration 配置

    Download FLTK 1.3.3 Download VS2010 I assume you've already installed VS2010 correctly. Compile the ...

  8. Useful Qt Examples

    Canvas API Basic Layouts Camera Example Video Widget Example Image Viewer Example Part 6 - Loading a ...

  9. [百科] - CreatePen()

    CreatePen编辑[声明]HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor);[说明]用指定的样式.宽度和颜色创建一个画笔[参数 ...

  10. asp.net 页面过程