[leetcode]Insert Interval @ Python
原题地址:https://oj.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]
.
解题思路:最简单的方法是将要插入的区间和原来的区间合在一起排序,然后按照merge intervals的方法来编程。
代码:
# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e class Solution:
# @param intervals, a list of Intervals
# @param newInterval, a Interval
# @return a list of Interval
# @should rewrite without sort!!!
def insert(self, intervals, newInterval):
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:
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]Insert Interval @ Python的更多相关文章
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Leetcode Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 57[LeetCode] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode] Insert Interval 二分搜索
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [LeetCode]Insert Interval 考虑多种情况
写太复杂了. 思想:确定带插入区间的每一个边界位于给定区间中的哪个位置,共同拥有5种情况 -1 |(0)_1_(2)| (3) 当中.0,1,2这三种情况是一样的. 确定每一个带插入区间的两个边界分 ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [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的其他问题 [ 做题通用的关键 ...
随机推荐
- ubuntu VNC中Xfce4中Tab键失效的解决方法
转:https://blog.csdn.net/xuezhisdc/article/details/48662435 说明 在Ubuntu Server 14.04上安装了xfce4桌面环境,但是却发 ...
- Python学习——Python基本数据类型(一)
python3 基本数据类型 Python3 中的变量不需要声明.每个变量在使用前都必须赋值,变量赋值以后该变量才会被创建. Python3 中有6个标准的数据类型:Number(数字);字符串(St ...
- spring boot学习总结(一)-- 基础入门 Hello,spring boot!
写在最前 SpringBoot是伴随着Spring4.0诞生的: 从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架: SpringBoot帮助开发者快速启动 ...
- Codeforces Round #258 (Div. 2) E. Devu and Flowers 容斥
E. Devu and Flowers 题目连接: http://codeforces.com/contest/451/problem/E Description Devu wants to deco ...
- windows pm2 开机启动
npm install pm2-windows-startup -g; pm2-startup install; pm2 kill; pm2 start ecosystem.config.js --o ...
- Mac的brew和brew cask区别以及安装brew cask
brew多用于命令行. brew cask主要用于有GUI的软件,例如VLC等等. brew cask是brew的一个子集,也就是一个扩展. 安装brew cask扩展: ruby -e " ...
- CSS动画简介
现在,我很少写介绍CSS的文章,因为感觉网站开发的关键还是在服务器端. 但是,CSS动画除外,它实在太有用了. 本文介绍CSS动画的两大组成部分:transition和animation.我不打算给出 ...
- 快递 API接口
http://www.kuaidi.com/openapi.html http://www.kuaidi100.com/ http://www.cnblogs.com/sususu3/p/577642 ...
- RPM软件包管理的查询功能 转
RPM软件包管理的查询功能: 命令格式 rpm {-q|--query} [select-options] [query-options] RPM的查询功能是极为强大,是极为重要的功能之一:举几个常用 ...
- android在代码中四种设置控件背景颜色的方法(包含RGB)
转载请注明出处: http://blog.csdn.net/fth826595345/article/details/9208771 TextView tText=(TextView) findVi ...