【leetcode】986. Interval List Intersections (双指针)
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 (双指针)的更多相关文章
- Leetcode 986. Interval List Intersections
暴搜.. class Solution(object): def intervalIntersection(self, A: List[Interval], B: List[Interval]) -& ...
- Java实现LeetCode #986 - Interval List Intersections
class Solution { public: vector<Interval> intervalIntersection(vector<Interval>& A, ...
- 【LeetCode】986. Interval List Intersections 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetco ...
- 【leetcode】986. Interval List Intersections
题目如下: Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted ...
- LC 986. Interval List Intersections
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...
- leetcode Insert Interval 区间插入
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...
- [LeetCode] Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...
- [leetcode]Insert Interval @ Python
原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, ...
随机推荐
- MarkDown学习随笔
MarkDown语法的学习 标题 设置标题方法是在前面加#号,一级标题(最大)是加#+空格 ,二级标题是加##+空格,之后的以此类推. 字体 在文本的前后分别加上一个星号表示斜体字 在文本的前后分 ...
- 关于docker中容器可以Ping通外网,真机无法Ping通容器的问题
首先我们要知道整体的框架结构,docker是我们安装在centos7上的,而centos7是安装在vmware上.其中docker中还有若干容器运行. 整体框架图如下: 我们将它分为两部分,一部分是d ...
- 基于 Istio 的全链路灰度方案探索和实践
作者|曾宇星(宇曾) 审核&校对:曾宇星(宇曾) 编辑&排版:雯燕 背景 微服务软件架构下,业务新功能上线前搭建完整的一套测试系统进行验证是相当费人费时的事,随着所拆分出微服务数量的不 ...
- 分布式技术-Zookeeper概述
概述 Zookeeper是一个开源的分布式的,为分布式应用提供协调服务的Apache项目 在大数据技术生态圈中,zookeeper(动物管理员),Hadoop(大象),Hive(蜜蜂),Pig(猪) ...
- MYSQL5.7下载安装图文教程
MYSQL5.7下载安装图文教程 一. MYSQL两种安装包格式 MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.zip格式相当于绿色版,不需要安装,只需解压缩之后就可以使用了,但 ...
- elasitcsearch单机版安装
1.下载压缩包 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz 2.解压修改配置文件 c ...
- c++学习笔记(四)
动态内存分配 使用new分配内存 1.指针的用处是在运行阶段分配未命名的内存以储存值. 2.在此情况下,只能通过指针来访问内存. int *ptr_int=new int;//在内存阶段为一个int值 ...
- 96.n-1位数
描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数. 输入 第一行为M,表示测试数据组数. 接下来M行,每行包含一个测试数据. 输出 输 ...
- 关于Java内存泄漏的介绍
翻译自这篇文章 Java一个最显著的优势就是它的内存管理.你只需要简单地创建对象,而Java垃圾收集器会负责内存的分配与释放.不过,事情并没有那么简单,因为在Java应用中时常会出现内存泄漏. 1. ...
- Ubuntu文件权限管理
1.介绍 第一个是设备文件类型 以c开头的是字符 以b开头的是块存储 ls-l: 读写可执行 rwx | rwx | rwx 这个文件所属的用用户 组内其他成员 其他不属于用户组的成员 2.文件权限修 ...