【leetcode】1272. Remove Interval
题目如下:
Given a sorted list of disjoint
intervals
, each intervalintervals[i] = [a, b]
represents the set of real numbersx
such thata <= x < b
.We remove the intersections between any interval in
intervals
and the intervaltoBeRemoved
.Return a sorted list of
intervals
after all such removals.Example 1:
Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
Output: [[0,1],[6,7]]Example 2:
Input: intervals = [[0,5]], toBeRemoved = [2,3]
Output: [[0,2],[3,5]]Constraints:
1 <= intervals.length <= 10^4
-10^9 <= intervals[i][0] < intervals[i][1] <= 10^9
解题思路:区间减法,判断两个区间的位置关系即可。
代码如下:
class Solution(object):
def removeInterval(self, intervals, toBeRemoved):
"""
:type intervals: List[List[int]]
:type toBeRemoved: List[int]
:rtype: List[List[int]]
"""
res = []
for start,end in intervals:
if end <= toBeRemoved[0] or start > toBeRemoved[1]:
res.append([start,end])
elif start == toBeRemoved[0] and end == toBeRemoved[1]:
continue
elif start == toBeRemoved[0] and end < toBeRemoved[1]:
continue
elif start == toBeRemoved[0] and end > toBeRemoved[1]:
res.append([toBeRemoved[1],end])
elif start >= toBeRemoved[0] and end == toBeRemoved[1]:
continue
elif start >= toBeRemoved[0] and end <= toBeRemoved[1]:
continue
elif start >= toBeRemoved[0] and end > toBeRemoved[1]:
res.append([toBeRemoved[1],end])
elif start < toBeRemoved[0] and end == toBeRemoved[1]:
res.append([start,toBeRemoved[0]])
elif start < toBeRemoved[0] and end > toBeRemoved[1]:
res.append([start,toBeRemoved[0]])
res.append([toBeRemoved[1],end])
elif start < toBeRemoved[0] and end < toBeRemoved[1]:
res.append([start, toBeRemoved[0]])
return res
【leetcode】1272. Remove Interval的更多相关文章
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【leetcode】1288. Remove Covered Intervals
题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...
- 【leetcode】1233. Remove Sub-Folders from the Filesystem
题目如下: Given a list of folders, remove all sub-folders in those folders and return in any order the f ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
随机推荐
- C++中map和unordered_map的用法
1. 简介 map和unordered_map都是c++中可以充当字典(key-value)来用的数据类型,但是其基本实现是不一样的. 2. map 对于map的底层原理,是通过红黑树(一种非严格意义 ...
- C++四种类型转换总结
C风格的强制类型转换很简单,均用 Type b = (Type)a 形式转换.C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用,如下表: 转换类型操作符 作用 const_cast 去 ...
- Spread.NET 表格控件 V12.1 正式发布
Spread.NET 表格控件 V12.1 正式发布 加入动态数组,让公式运算更具效率 Spread.NET 是一个在功能和布局上与 Excel 高度类似的 .NET表格控件,目前已广泛应用于财务.预 ...
- 利用commons-pool2自定义对象池
一.为什么使用对象池 恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.commons-pool2是Apache下一个开源的公共资源池.我们可以根据它来快速的建立 ...
- plpython 中文分词Windows 版
windows 下安装版本匹配python-3.4.3.amd64.msipostgresql-10.1-2-windows-x64.exe create language plpython3u;se ...
- hashMap怎样解决hash冲突
通过链表的方式处理: java1.7是单向链表 jvav1.8在数量小于8时是单向链表,大于8就是红黑树,查找方式遍历判断 解决冲突的方式很多,例如再hash,再散列(开放地址法,探测再散列)
- python-day37(正式学习)
前景回顾 抢票系统的代码优化,使用了Lock类 from multiprocessing import Process,Lock import os,time,json with open('user ...
- java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...
- 什么是blazor
blazor是一个微软推出的基于webassembly和C#(面向对象) 以及F#(面向函数)的前端框架 它类似vue react anglar的单页前端框架 只是他不再使用js 或typescrip ...
- 将Abp的UnitTest中的InMemory改为SQLite in memory
添加nuget包 Microsoft.EntityFrameworkCore.Sqlite 添加ServiceCollectionRegistrarSqlite public static class ...