当前位置:网站首页>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
边栏推荐
猜你喜欢

Visualization of neural network structure in different frames

Flatten of cnn-lstm

pyechart绘制多条y轴折线图

学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用

Ehcache配置资料,方便自己查

Leetcode daily question - 515 Find the maximum value in each tree row

基于 Apache APISIX 的自动化运维平台
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis

Bitbucket failed to pull the warehouse Using SSH

什么是接口?什么是接口测试?
随机推荐
How to analyze the relationship between enterprise digital transformation and data asset management?
Fix the simulator that cannot be selected by flutter once
Ehcache配置资料,方便自己查
3. integrate listener
Flatten of cnn-lstm
CNN-LSTM的flatten
LeetCode每日一题——30. 串联所有单词的子串
Employee salary management system
Leetcode daily question - 30 Concatenate substrings of all words
The further application of Li Kou tree
Anr no response introduction
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
RT-Thread线程同步与线程通信
【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
Real number operation
With a market value of $120billion, how did intuit, an old tax giant, do it?
How to understand the fast iteration of cloud native database?
ComparisonChain-文件名排序
题解 Pie(POJ3122)超详细易懂的二分入门
图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer