码迷,mamicode.com
首页 > 其他好文 > 详细

1272. Remove Interval

时间:2019-12-03 01:34:33      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:list   put   cti   nal   https   com   interval   number   ||   

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. 右边相交

后两种判断后添加即可。

1272. Remove Interval

标签:list   put   cti   nal   https   com   interval   number   ||   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!