当前位置:网站首页>LeetCode986. 区间列表的交集
LeetCode986. 区间列表的交集
2022-06-28 20:59:00 【Yuyy】
本文最后更新于 484 天前,其中的信息可能已经有所发展或是发生改变。
一、思路
这个区间问题,在两个列表里,互相比较。采用双指针是实现这个过程。
分为两种情况,相交和不相交。相交情况,end取两个区间的最大值。不相交时,看哪个区间大,当前的end是小的区间的最大值。下一对start,end取大的个区间。
什么时候指针移动呢?根据两个当前区间的最大值,小的个指针就往前移。因为一直在进行两个区间的比较,所以趋向于两个指针一起往前走。
二、问题
给定两个由一些 闭区间 组成的列表,firstList 和 secondList ,其中 firstList[i] = [starti, endi] 而 secondList[j] = [startj, endj] 。每个区间列表都是成对 不相交 的,并且 已经排序 。
返回这 两个区间列表的交集 。
形式上,闭区间[a, b](其中 a <= b)表示实数 x 的集合,而 a <= x <= b 。
两个闭区间的 交集 是一组实数,要么为空集,要么为闭区间。例如,[1, 3] 和 [2, 4] 的交集为 [2, 3] 。
示例 1:
输入:firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
输出:[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]示例 2:
输入:firstList = [[1,3],[5,9]], secondList = []
输出:[]示例 3:
输入:firstList = [], secondList = [[4,8],[10,12]]
输出:[]示例 4:
输入:firstList = [[1,7]], secondList = [[3,10]]
输出:[[3,7]]提示:
0 <= firstList.length, secondList.length <= 1000firstList.length + secondList.length >= 10 <= starti < endi <= 109endi < starti+10 <= startj < endj <= 109endj < startj+1
Related Topics
- 双指针
\n
- 132
- 0
三、代码
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
ArrayList<int[]> list = new ArrayList<>();
int pointer = 0;
int pointer1 = 0;
while (pointer < firstList.length && pointer1 < secondList.length) {
int n1 = Math.min(firstList[pointer][1], secondList[pointer1][1]);
int n0 = Math.max(firstList[pointer][0], secondList[pointer1][0]);
if (n1 >= n0) {
list.add(new int[]{n0, n1});
}
if (firstList[pointer][1] > secondList[pointer1][1]) {
pointer1++;
} else {
pointer++;
}
}
return list.toArray(new int[list.size()][]);
}Post Views: 260
边栏推荐
- 国产数据库名录一览
- ThreadLocal principle
- 如何使用 DataAnt 监控 Apache APISIX
- input separator
- 请问同业存单是否靠谱,安全吗
- Please allow the "imperfection" of the current domestic Tob
- Understanding of incomplete types
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- Ehcache配置资料,方便自己查
- Explanation of memory dump triggered by software watchdog and anr
猜你喜欢

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

基于 Apache APISIX 的自动化运维平台

Leetcode daily question - 515 Find the maximum value in each tree row
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)

postman简介与安装步骤

MongoDB——副本集与分片
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

什么是接口?什么是接口测试?

如何使用 DataAnt 监控 Apache APISIX

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
随机推荐
如何添加 logs来debug ANR 问题
LeetCode每日一题——710. 黑名单中的随机数
Apisik helps Middle East social software realize localized deployment
Résumé de la stabilité
How to do a good job in customer's successful bottom design | tob Master Course
resilience4j 重试源码分析以及重试指标采集
Visualization of neural network structure in different frames
Relevant calculation of sphere, etc
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
员工薪资管理系统
How to open an account in great wisdom? Is it safe
Learn Tai Chi maker mqtt Chapter 2 (VIII) esp8266 mqtt user password authentication
Comparisonchain file name sort
LeetCode每日一题——522. 最长特殊序列 II
[book club issue 13] packaging format of video files
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
力扣树的进一步应用
How to "calculate" in the age of computing power? The first mover advantage of "convergence of computing and networking" is very important!
Real number operation