博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode/python3/hard/57】Insert Interval
阅读量:2171 次
发布时间:2019-05-01

本文共 884 字,大约阅读时间需要 2 分钟。

题目

基本思路

首先,将newInterval添加到intervals中,然后按照区间开始数值,进行排序,最后将对于是否需要merge进行判断,分情况处理

花费了额外的存储空间

实现代码

# Definition for an interval.# class Interval:#     def __init__(self, s=0, e=0):#         self.start = s#         self.end = eclass Solution:    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 = []                for i in range(length):            if res == []:                res.append(intervals[i])            else:                if res[-1].start <= intervals[i].start <= res[-1].end:                    res[-1].end = max(intervals[i].end,res[-1].end)                else:                    res.append(intervals[i])        return res
你可能感兴趣的文章
Leetcode C++《热题 Hot 100-28》19.删除链表的倒数第N个节点
查看>>
Leetcode C++《热题 Hot 100-29》22.括号生成
查看>>
Leetcode C++《热题 Hot 100-44》102.二叉树的层次遍历
查看>>
Leetcode C++《热题 Hot 100-47》236.二叉树的最近公共祖先
查看>>
Leetcode C++《热题 Hot 100-48》406.根据身高重建队列
查看>>
《kubernetes权威指南·第四版》第二章:kubernetes安装配置指南
查看>>
Leetcode C++《热题 Hot 100-49》399.除法求值
查看>>
Leetcode C++《热题 Hot 100-51》152. 乘积最大子序列
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>
Leetcode C++ 《第181场周赛-2》 1390. 四因数
查看>>
阿里云《云原生》公开课笔记 第一章 云原生启蒙
查看>>
阿里云《云原生》公开课笔记 第二章 容器基本概念
查看>>
阿里云《云原生》公开课笔记 第三章 kubernetes核心概念
查看>>
阿里云《云原生》公开课笔记 第四章 理解Pod和容器设计模式
查看>>
阿里云《云原生》公开课笔记 第五章 应用编排与管理
查看>>
阿里云《云原生》公开课笔记 第六章 应用编排与管理:Deployment
查看>>
阿里云《云原生》公开课笔记 第七章 应用编排与管理:Job和DaemonSet
查看>>
阿里云《云原生》公开课笔记 第八章 应用配置管理
查看>>
阿里云《云原生》公开课笔记 第九章 应用存储和持久化数据卷:核心知识
查看>>
linux系统 阿里云源
查看>>