You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

A closed interval [a, b] (with a <= b) denotes the set of real numbers x with a <= x <= b.

The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For example, the intersection of [1, 3] and [2, 4] is [2, 3].

Example 1:

Input: firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]

这道题需要求两个数组的公共区间的集合,如果用暴力法的话时间复杂度就是o(mn),如果用双指针的话时间复杂度就是o(m+n),这道题用双指针很有意思,需要注意的是两个指针的更新规则。
class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& firstList, vector<vector<int>>& secondList) {
// 暴力法的时间复杂就是o(mn)
// 利用双指针检索 时间复杂度为o(m+n) 用i j 分别指向firstlist 以及secondlist 中的元素
vector<vector<int>> res;
int m=firstList.size(),n=secondList.size();
int i=0,j=0; //双指针
while(i<m && j<n){
if(firstList[i][1]<secondList[j][0]) i++;
else if(firstList[i][0]>secondList[j][1]) j++; //没有交集 小的区间向后移动
else{
res.push_back({max(firstList[i][0],secondList[j][0]),min(firstList[i][1],secondList[j][1])}); //存储交集区间
if(firstList[i][1]<secondList[j][1]) i++;
else if (firstList[i][1]>secondList[j][1]) j++;
else{
i++;
j++;
}
}
}
return res; }
};


【leetcode】986. Interval List Intersections (双指针)的更多相关文章

  1. Leetcode 986. Interval List Intersections

    暴搜.. class Solution(object): def intervalIntersection(self, A: List[Interval], B: List[Interval]) -& ...

  2. Java实现LeetCode #986 - Interval List Intersections

    class Solution { public: vector<Interval> intervalIntersection(vector<Interval>& A, ...

  3. 【LeetCode】986. Interval List Intersections 解题报告(C++)

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

  4. 【leetcode】986. Interval List Intersections

    题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...

  5. LC 986. Interval List Intersections

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

  6. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  7. [LeetCode] Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

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

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

  9. [leetcode]Insert Interval @ Python

    原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...

随机推荐

  1. 第01课 OpenGL窗口(2)

    下一段包括了所有的绘图代码.任何您所想在屏幕上显示的东东都将在此段代码中出现.以后的每个教程中我都会在例程的此处增加新的代码.如果您对OpenGL已经有所了解的话,您可以在 glLoadIdentit ...

  2. (二)FastDFS 高可用集群架构学习---搭建

    一.单group 单磁盘 的 FastDFS 集群 a.前期准备 1.系统软件说明: 名称 说明 CentOS 7.x(安装系统) libfastcommon FastDFS分离出的一些公用函数包 F ...

  3. 【前端工具】nodejs+npm+vue 安装(windows)

    预备 先看看这几个是干嘛的,相互的关系是啥. nodejs是语言,类比到php. npm是个包管理,类比到composer. vue是个框架,类比到laravel. webpack是个打包工具. 先下 ...

  4. JavaScript高阶函数之filter、map、reduce

    JavaScript高阶函数 filter(过滤) 用法: 用于过滤,就是把数组中的每个元素,使用回调函数func进行校验,回调函数func返回一个布尔值,将返回值为 true 的元素放入新数组 参数 ...

  5. 从环境搭建到回归神经网络案例,带你掌握Keras

    摘要:Keras作为神经网络的高级包,能够快速搭建神经网络,它的兼容性非常广,兼容了TensorFlow和Theano. 本文分享自华为云社区<[Python人工智能] 十六.Keras环境搭建 ...

  6. Swift-Framework Error(一)桥接文件

    摘要 Xcode 编译工程代码时,出现编译错误时除了红色图标外,还会附送几句英文文本. 常规操作拷贝英文文本,放到搜索框中找答案,但是读懂这几句话能事半功倍. 项目中如果有 OC 和 Swift 两种 ...

  7. Part 61 to 63 Talking about partial class and partial method in C#

    部分类和部分方法 部分类是用partial声明的类,它允许我们把一个类分割成两个或多个类,当应用程序编译的完成的时候,多个部分类会结合成一个类,同时partial关键字也可以用于声明结构和接口. 那么 ...

  8. python实现图像加载与保存,窗口创建与销毁,图片常用属性,ROI,通道的分离与合并,对比度和亮度

    目录: (一)图像加载与保存 (二)图像显示窗口创建与销毁 (三)图片的常用属性的获取 (四)生成指定大小的矩形区域(ROI) (五)图片颜色通道的分离与合并 (六)两张图片相加,改变对比度和亮度 ( ...

  9. [bzoj5508]甲苯先生的字符串

    首先定义状态f[i][j]表示长度为i的串以j为结尾有多少符合条件的串,发现$f[i][j]=\sum f[i-1][k]$(j和k可以相邻),这个用矩阵乘法优化一下即可. 1 #include< ...

  10. [atACL001F]Center Rearranging

    有一个(比较显然又有点假的)结论:最优方案中(若存在),每一个数(指$3n$个)最多被移动1次 先$o(n^{2})$枚举移动到队首和队尾的操作次数(即目标状态的一个前缀和后缀),判定能否合法 首先, ...