题目如下:

Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the set of real numbers x such that a <= x < b.

We remove the intersections between any interval in intervals and the interval toBeRemoved.

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

  1. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  2. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

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

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

  4. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  5. 【leetcode】1288. Remove Covered Intervals

    题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...

  6. 【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 ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  8. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  9. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

随机推荐

  1. ORACLE 左连接 右连接 内连接 外连接 全连接 五中表连接方式

    1.左连接 :left join 2.右连接:right join 3.内连接:inner join 4.外连接:outer join 5.全连接:full join

  2. ABC136E Max GCD

    Thinking about different ways of thinking. --- LzyRapx 题目 思路比较容易想到. Observations: 每次操作过后和不变. 枚举和的因子 ...

  3. 用python操作mysql数据库

    数据库的安装和连接 PyMySQL的安装 pip install PyMySQL python连接数据库 import pymysql db = pymysql.connect("数据库ip ...

  4. python_0基础开始_day11

    第十一节 一,函数名的第一类对象 函数名当作值,赋值给变量 print(函数名) 查看看书的内存地址 函数名可以当作容器中的元素 lis = []dic = {}def func():    prin ...

  5. Leetcode简单题

    # Title Solution Acceptance Difficulty Frequency     1 Two Sum       44.5% Easy     2 Add Two Number ...

  6. ionic 提示 Error: Could not find gradle wrapper within Android SDK.

    Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK. 到And ...

  7. 转换byte(十进制)为图形(大小写字母,符号)

    /** * 转换byte(十进制)为字母 */ public static String ByteToLetter (byte[] chars) { String Letter = "&qu ...

  8. 11 Mysql之配置双主热备+keeepalived.md

    准备 1. 双主 master1 192.168.199.49 master2 192.168.199.50 VIP 192.168.199.52 //虚拟IP 2.环境 master:nginx + ...

  9. String,到底创建了多少个对象?

      String str=new String("aaa"); <span style="font-size:14px;">String str=n ...

  10. Delphi MainMenu组件