1272. Remove Interval

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 {
    public List<List<Integer>> removeInterval(int[][] intervals, int[] toBeRemoved) {
        List<List<Integer>> ans = new ArrayList<>();
        for (int[] i : intervals) {
            if (i[1] <= toBeRemoved[0] || i[0] >= toBeRemoved[1]) { // no overlap.
                ans.add(Arrays.asList(i[0], i[1]));
            }else { // i[1] > toBeRemoved[0] && i[0] < toBeRemoved[1].
                if(i[0] < toBeRemoved[0]) // left end no overlap.
                    ans.add(Arrays.asList(i[0], toBeRemoved[0]));
                if (i[1] > toBeRemoved[1]) // right end no overlap.
                    ans.add(Arrays.asList(toBeRemoved[1], i[1]));
            }
        }
        return ans;
    }
}

https://leetcode.com/problems/remove-interval/discuss/440799/JavaPython-3-12-and-11-liners-w-brief-comments-and-analysis.

有点像merge interval,但是更简单。

对每个数组有三种情况,

1. 不相交(说明没有要删除的),把数组添加进list

2. 左边相交

3. 右边相交

后两种判断后添加即可。

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/11974184.html

时间: 2024-11-09 15:56:23

1272. Remove Interval的相关文章

oracle存储过程与job

首先存储过程是干什么的?job是干什么的? 存储过程:相当于一个复杂的sql,用来执行自定义的复杂的功能,创建了之后会存入一个表里,可以通过job来执行存储过程,实现我们需要的功能 job:实际上就是数据库内置的定时任务,可以设置存储过程什么时间执行的这么一种功能,是数据库自带的, ==========================存储过程==========================存储过程可以通过以写复杂sql的形式来实现自定义的特殊功能,由自己书写定义,定义后存入内置的表里定义格式

AjaxFileUpload 方法与原理分析

AjaxFileUpload需求 传统的form表单方式上传文件,  必然会刷新整个页面. 那么在不刷新界面的情况下实现文件的上传呢? 在 HTML4下, 聪明的程序员们发明了 ajax file upload 方式(form + hidden iframe方式), 为本文介绍的对象.  在HTML5中XMLHttpRequest实现了异步上传文件(介绍见 http://wenzhixin.net.cn/2013/11/28/ajax_file_upload_html5). 开源项目 官网为 h

leetcode 57 Insert Interval ----- java

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 [

352. Data Stream as Disjoint Interval

Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1

Java for LeetCode 057 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

Insert Interval &amp; Merge Intervals

Insert Intervals Given a non-overlapping interval list which is sorted by start point. Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Example Insert [2, 5] into [[1,2], [5,9]],

【leetcode】1288. Remove Covered Intervals

题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the list. Interval [a,b) is covered by interval [c,d) if and only if c <= a and b <= d. After doing so, return the number of remaining intervals. Example 1

android导入其他工程源码包后出现大量错误提示remove @Override annotation 的解决办法

问题描述: GitHub 下载源码后将其com包内容导入自己android项目中,大量出现错误修改提示 remove @Override annotation 问题解法: 1. 2. 3. 将Compiler compliance level:设置在1.6及以上即可

JsSIP.UA.JsSIP 总是返回错误:422 Session Interval Too Small

在JsSIP 中 JsSIP.UA.call 总是 返回错误:422 Session Interval Too Small 关于错详情在这篇文章中解释的比较详尽:http://www.cnblogs.com/yoyotl/p/4980817.html 但是没有JsSIP的解决方法 具体的解决方法如下: JsSIP.js中的搜索 SESSION_EXPIRES: 把这个参数改大,  大于120就行了, (要是还不行,就继续加大吧^.^) SESSION_EXPIRES: 90 加大: SESSIO