当前位置:网站首页>LeetCode986. Intersection of interval lists
LeetCode986. Intersection of interval lists
2022-06-28 21:05:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
This interval problem , In two lists , Compare with each other . use Double pointer It's the realization of this process .
There are two cases , Intersect and disjoint . Intersection ,end Take the maximum of the two intervals . When they don't intersect , See which interval is big , Current end Is the maximum of a small interval . Next pair start,end Take the larger interval .
When does the pointer move ? According to the maximum value of the two current intervals , The small pointer moves forward . Because we have been comparing the two intervals , So it tends to go forward with two pointers .
Two 、 problem
Given two by some Closed interval A list of components ,firstList and secondList , among firstList[i] = [starti, endi] and secondList[j] = [startj, endj] . Each interval list is paired Disjoint Of , also It's sorted .
Back here The intersection of two interval lists .
Formally , Closed interval [a, b]( among a <= b) For real numbers x Set , and a <= x <= b .
Of two closed intervals intersection Is a set of real numbers , Or it's an empty set , Either it's a closed interval . for example ,[1, 3] and [2, 4] The intersection of 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]]Example 2:
Input :firstList = [[1,3],[5,9]], secondList = []
Output :[]Example 3:
Input :firstList = [], secondList = [[4,8],[10,12]]
Output :[]Example 4:
Input :firstList = [[1,7]], secondList = [[3,10]]
Output :[[3,7]]Tips :
0 <= firstList.length, secondList.length <= 1000firstList.length + secondList.length >= 10 <= starti < endi <= 109endi < starti+10 <= startj < endj <= 109endj < startj+1
Related Topics
- Double pointer
\n
- 132
- 0
3、 ... and 、 Code
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
边栏推荐
- How to open an account in great wisdom? Is it safe
- The principle and source code analysis of Lucene index construction
- LeetCode每日一题——324. 摆动排序 II
- 大智慧上怎么进行开户啊, 安全吗
- Ref attribute, props configuration, mixin mixing, plug-in, scoped style
- Bitbucket 使用 SSH 拉取仓库失败的问题
- List of domestic database directory
- Understanding of incomplete types
- 题解 Pie(POJ3122)超详细易懂的二分入门
- Input and output real data
猜你喜欢

Query rewriting for opengauss kernel analysis

MySQL system error occurred 1067

我也差点“跑路”

Leetcode 36. 有效的数独(可以,一次过)

How to analyze the relationship between enterprise digital transformation and data asset management?

Mongodb - replica set and sharding

Flatten of cnn-lstm

阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践

视频号如何下载视频?来看超简单方法!
![[learning notes] Introduction to principal component analysis](/img/24/a760d1cd095a967ef258b623eb465c.png)
[learning notes] Introduction to principal component analysis
随机推荐
LeetCode121. 买卖股票的最佳时机
How do I download videos? Look at the super simple method!
LeetCode986. 区间列表的交集
Is the rapid SSL wildcard certificate genuine in 1981
LeetCode每日一题——710. 黑名单中的随机数
How to recover after Oracle delete accidentally deletes table data
T检验(检验两个总体的均值差异是否显著)
How to add logs to debug anr problems
[learning notes] Introduction to principal component analysis
Keyword long
Bitbucket 使用 SSH 拉取仓库失败的问题
The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
with torch.no_grad():的使用原因
LeetCode117. 填充每个节点的下一个右侧节点指针_II
1. integrate servlets
Query rewriting for opengauss kernel analysis
With a market value of $120billion, how did intuit, an old tax giant, do it?
LeetCode877. 石子游戏
【读书会第13期】视频文件的封装格式
LeetCode:合并K个升序链表_23