问题描述

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

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

(形式上,闭区间 [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. CF1043A Elections 题解

    Content 有两个人参加选举,其中已知 \(n\) 位选民投给第二个人的票数为 \(a_1,a_2,a_3,...,a_n\).第一个人很想赢,所以想通过调整每位选民只能投的票数 \(k\) 来让 ...

  2. /etc/resolv.conf文件中的search项作用;如何保持resolv.conf文件内容不被修改

    /etc/resolv.conf文件中的search项作用 resolv.conf文件中有search项时,主机名解析规则顺序: DNS配置文件如下: # cat /etc/resolv.conf ; ...

  3. .NET 多条件动态参数查询方法 - SqlSugar ORM

    1.简单多条件多动参数 创建数据库对象 //创建数据库对象 SqlSugarClient SqlSugarClient db = new SqlSugarClient(new ConnectionCo ...

  4. 痞子衡嵌入式:把玩i.MXRT1062 TencentOS Tiny EVB_AIoT开发板(2) - 在Flash调试及离线启动

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是i.MXRT1062 TencentOS Tiny EVB_AIoT开发板在Flash调试与离线启动. 腾讯 TencentOS 团队于 ...

  5. Visiual Studio之c++项目瘦身(删除中间项)

    欢迎指正 本文主要涉及 Visiual Studio(简称VS) 创建的c++项目 和 windows下批处理相关点. 1.中间项 A.VS创建的c++项目,生成后,会有许多中间项,包括项目生成的中间 ...

  6. 【LeetCode】1065. Index Pairs of a String 解题报告(C++)

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

  7. 【LeetCode】304. Range Sum Query 2D - Immutable 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 预先求和 相似题目 参考资料 日期 题目地址:htt ...

  8. 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)

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

  9. Boosting Adversarial Training with Hypersphere Embedding

    目录 概 主要内容 代码 Pang T., Yang X., Dong Y., Xu K., Su H., Zhu J. Boosting Adversarial Training with Hype ...

  10. On the Optimization of Deep Networks: Implicit Acceleration by Overparameterization

    目录 引 主要内容 定理1 Claim 1 Claim 2 定理2 证明 定理1的证明 Claim 1 的证明 Kronecker product (克罗内克积) Theorem 2 的证明 代码 A ...