问题描述

给定两个由一些 闭区间 组成的列表,每个区间列表都是成对不相交的,并且已经排序。

返回这两个区间列表的交集。

(形式上,闭区间 [a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b。两个闭区间的交集是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3]。)
示例:

输入:A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

代码

class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
int Ai = 0,Bi = 0,An = A.size(),Bn = B.size();
vector<vector<int>> ans;
while(Ai < An && Bi < Bn)
{
//没有交集的情况只有两种 A[Ai][0] > B[Bi][1] or B[Bi][0] > A[Ai][1]
//有交集则要去个非,实际上对应四种情况
/* 1.|--------------------| A
|-----------| B
2.|--------------------| A
|------------------| B
3. |-----------| A
|--------------------| B
4. |-----------| A
|--------------------| B
*/
if(A[Ai][0] <= B[Bi][1] && B[Bi][0] <= A[Ai][1])
{
vector<int>tmp(2);
tmp[0] = max(A[Ai][0],B[Bi][0]);
tmp[1] = min(A[Ai][1],B[Bi][1]);
ans.push_back(tmp);
}
//指标是否前进取决于末端
if(A[Ai][1] > B[Bi][1]){
Bi++;
}
else
Ai++;
}
return ans;
}
};

结果

执行用时:84 ms, 在所有 C++ 提交中击败了43.17%的用户
内存消耗:18.9 MB, 在所有 C++ 提交中击败了22.76%的用户

leetcode 986. 区间列表的交集的更多相关文章

  1. [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

  2. c#得出两个列表的交集

    c#提供了Intersect来得到两个列表的交集,它是通过使用默认的相等比较器对值进行比较生成两个序列的交集,定义为: public static IEnumerable<TSource> ...

  3. LeetCode 349,350 数组的交集

    LeetCode 349: package com.lt.datastructure.Set; import java.util.ArrayList; import java.util.LinkedH ...

  4. Python 基础 - 随机列表的交集

    # -*- coding: utf-8 -*- #author:v def shiyiwenpapa(): def sywmemeda(l): #冒泡排序 length = len(l) for i ...

  5. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  6. 【LeetCode】区间合并

    给定一组区间,将所有区间重叠的部分合并起来. e.g. 给出 { [1, 3], [2, 6], [8, 10], [15, 18] },返回 { [1, 6], [8, 10], [15, 18] ...

  7. leetcode 两个数组的交集 II

    给定两个数组,写一个方法来计算它们的交集. 例如: 给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. /** * @param {number[] ...

  8. leetcode合并区间

    合并区间     给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: ...

  9. Leetcode 327.区间和的个数

    区间和的个数 给定一个整数数组 nums,返回区间和在 [lower, upper] 之间的个数,包含 lower 和 upper.区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的 ...

随机推荐

  1. LuoguP2108 学英语 题解

    Content 给出整数 \(x\) 的英文写法,求出这个整数 \(x\). 规则详见题面. 数据范围:\(|x|\leqslant 999999999\)(\(9\) 个 \(9\)). Solut ...

  2. LuoguB2106 矩阵转置 题解

    Content 给定一个 \(n\times m\) 的矩阵 \(A\),求其转置 \(A^\text T\). 数据范围:\(1\leqslant n,m\leqslant 100\). Solut ...

  3. LuoguP6857 梦中梦与不再有梦 题解

    Update \(\texttt{2020.10.20}\) 增加了证明.感谢@东北小蟹蟹(dbxxxqwq)的提醒. Content 有一个 \(n\) 个点的无向图,每两个点之间都有一条边直接相连 ...

  4. 大小端(内存、寄存器、CPU)

    CPU能进行32位操作,关键是寄存器有32位,数据总线也有32位. 为了表示方便,我们可以把32位寄存器从左到右,与内存中一个双字的四个字节地址从低到高对应. 如果CPU把寄存器的左端定义为高位,则带 ...

  5. Maven配置使用阿里云镜像

    在settings.xml文件中的mirrors下添加mirror标签 <!-- 阿里云仓库 --> <mirror> <id>alimaven</id> ...

  6. Linux C(++)获取可执行程序完整路径

    代码 #include <sys/statfs.h> #include <string> #include <iostream> #include <limi ...

  7. 平衡二叉树(c++)实现(存在问题:插入节点后,问题:调整树的结构存在问题)

    !!版权声明:本文为博主原创文章,版权归原文作者和博客园共有,谢绝任何形式的 转载!! 作者:mohist 更新那时间: 22:13  03-02-2020  逻辑存在问题:插入节点后,调整数的结构不 ...

  8. nim_duilib(6)之listbox

    introduction 更多控件用法,请参考 here 和 源码. 本文的代码基于这里 本文将演示listbox的添加,删除,删除选中项,添加到指定位置等常用功能. xml文件添加代码 基于上一篇, ...

  9. 防止 jar 包被反编译

    1.隔离Java程序 最简单的方法就是让用户不能够访问到Java Class程序,这种方法是最根本的方法,具体实现有多种方式.例如,开发人员可以将关键的Java Class放在服务器端,客户端通过访问 ...

  10. Robust Pre-Training by Adversarial Contrastive Learning

    目录 概 主要内容 代码 Jiang Z., Chen T., Chen T. & Wang Z. Robust Pre-Training by Adversarial Contrastive ...