【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, ...
随机推荐
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- Spring Cloud Alibaba 使用 feign 和 rebion 进行服务消费
微服务的服务消费,一般是使用 feign 和 rebion 调用服务提供,进行服务的消费,本文将实战使用代码讲解服务的消费. 微服务环境的搭建 创建一个 springboot 项目,springboo ...
- Buildroot 用户手册 (中文)
文章目录 I. Getting started 1. About Buildroot 2. System requirements 2.1. Mandatory packages 2.2. Optio ...
- django improperly configured
ImproperlyConfigured: You must either define the environment variable DJANGO_SETTINGS_MODULE or call ...
- MyCat读写分离+MySql主从(一主一从)
数据库一直在项目担当着一位核心的角色,是所有项目结构中的底层,说白了,我们程序员进行项目开发都是在和数据打交道,而数据都是保存在数据库中,如mysql.oracle.postgresql等等,如果一个 ...
- 全球首发-基于.NET 6长线支持Zoomla!逐浪CMS v8.6.0正式发布
传送门: https://www.z01.com/down/3778.shtml 全新Zoomla!逐浪CMS v8.6.0 全于首个基于.net 6长线支持的CMS-Zoomla!逐浪CMS v8. ...
- Java 获取PDF数字签名证书信息
PDF文档中可添加数字签名,在添加签名前,需要准备可信任签名证书.对文档中已有的签名,可验证书签是否有效.也可通过一定方法来获取数字签名或者签名证书信息.下面以Java代码示例展示如何读取签名的证书信 ...
- 算法题-n月后兔子数量
有一对兔子,从出生后第5个月起每个月都生一对兔子,小兔子长到第5个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? public class test3 { public stati ...
- Dapr-绑定构建块
前言: 前篇-发布订阅文章对Dapr的订阅/发布进行了解,本篇继续对 绑定 构建块进行了解. 一.简介: Dapr 资源绑定使服务能够跨即时应用程序外部的外部资源集成业务操作. 来自外部系统的事件可能 ...
- OWASP-Top5-(Security Misconfiguration 安全配置错误)
概述 从上一版的第 6 位开始,90% 的应用程序都经过了某种形式的错误配置测试.随着更多转向高度可配置的软件,看到这一类别上升也就不足为奇了.值得注意的CWE包括CWE-16 Configurati ...