当前位置:网站首页>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
边栏推荐
- With a market value of $120billion, how did intuit, an old tax giant, do it?
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- 稳定性总结
- RT-Thread线程同步与线程通信
- The further application of Li Kou tree
- Visualization of neural network structure in different frames
- rapid ssl通配符证书八百一年是正版吗
- Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
- Leetcode daily question - 522 Longest special sequence II
- 【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
猜你喜欢

Visualization of neural network structure in different frames

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

Lucene构建索引的原理及源代码分析

Leetcode 36. Effective Sudoku (yes, once)

ThreadLocal原理
How to recover after Oracle delete accidentally deletes table data

数据资产为王,如何解析企业数字化转型与数据资产管理的关系?

Automatic operation and maintenance platform based on Apache APIs

with torch. no_ Grad(): reason for using

Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
随机推荐
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
How to open an account in great wisdom? Is it safe
mysql-发生系统错误1067
How to do a good job in customer's successful bottom design | tob Master Course
RT-Thread线程同步与线程通信
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用
pyechart绘制多条y轴折线图
LeetCode986. 区间列表的交集
题解 Pie(POJ3122)超详细易懂的二分入门
[learning notes] factor analysis
API 网关 Apache APISIX 助力雪球双活架构演进
精通数据分析能力,收入翻倍?什么才是最强竞争力
LeetCode560. 和为K的子数组
Pie (poj3122) super detailed and easy to understand binary introduction
Ehcache配置资料,方便自己查
Keyword long
I almost ran away
LeetCode每日一题——30. 串联所有单词的子串
嵌入式中 动态阿拉伯语字符串 转换 LCD显示字符串【感谢建国雄心】
Leetcode 36. Effective Sudoku (yes, once)