问题描述

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

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

(形式上,闭区间 [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. Java 常用类库一,main方法传参String[] args;获取输入Scanner ;hasNext();hasNextInt()

    1. main方法传参 package com.zmd.common_class_libraries; /** 给mian方法传参测试 */ public class MainArgsTest { p ...

  2. mysql绿色版添加服务

  3. C++11 新特性:enable_shared_from_this

    enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为:template< class T > class enable_share ...

  4. JAVA实现根据图片生成缩略图、裁剪、压缩图片

    依赖(用来复制文件,可以根据自己的来) <dependency> <groupId>commons-io</groupId> <artifactId>c ...

  5. 【LeetCode】311. Sparse Matrix Multiplication 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 科学计算库numpy 日期 题目地址:https ...

  6. 【九度OJ】题目1206:字符串连接 解题报告

    [九度OJ]题目1206:字符串连接 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1206 题目描述: 不借用任何字符串库函数实现无 ...

  7. spoj - ACTIV - Activities

    ACTIV - Activities Ana likes many activities. She likes acrobatics, alchemy, archery, art, Arabic da ...

  8. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  9. Lucky Substrings

    而在26以内且属于fibonacci数列的数为1,2,3,5,8,13,21时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 A string s is LUCKY if ...

  10. MySQL中视图的定义、原理--触发器

    视图概述 视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用 ...