LeetCode 1272. 删除区间
发布日期:2021-07-01 03:30:05 浏览次数:2 分类:技术文章

本文共 1107 字,大约阅读时间需要 3 分钟。

文章目录

1. 题目

给你一个 有序的 不相交区间列表 intervals 和一个要删除的区间 toBeRemoved, intervals 中的每一个区间 intervals[i] = [a, b] 都表示满足 a <= x < b 的所有实数 x 的集合。

我们将 intervals 中任意区间与 toBeRemoved 有交集的部分都删除

返回删除所有交集区间后, intervals 剩余部分的 有序 列表。

示例 1:输入:intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]输出:[[0,1],[6,7]]示例 2:输入:intervals = [[0,5]], toBeRemoved = [2,3]输出:[[0,2],[3,5]] 提示:1 <= intervals.length <= 10^4-10^9 <= intervals[i][0] < intervals[i][1] <= 10^9

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/remove-interval

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

class Solution {
public: vector
> removeInterval(vector
>& intervals, vector
& toBeRemoved) {
vector
> ans; int l = toBeRemoved[0], r = toBeRemoved[1]; for(auto& inter : intervals) { if(inter[1] <= l || inter[0] >= r)//不相交 ans.push_back(inter); else//相交有两种情况 { if(inter[0] < l) ans.push_back({ inter[0], l}); if(inter[1] > r) ans.push_back({ r, inter[1]}); } } return ans; }};

204 ms 32.2 MB


我的CSDN

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

Michael阿明

转载地址:https://michael.blog.csdn.net/article/details/107575663 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode 549. 二叉树中最长的连续序列(树上DP)
下一篇:LeetCode 1121. 将数组分成几个递增序列

发表评论

最新留言

很好
[***.229.124.182]2024年04月30日 07时28分08秒