当前位置:网站首页>力扣986.区间列表的交集
力扣986.区间列表的交集
2022-07-24 05:23:00 【u_guess_】
力扣986.区间列表的交集(自己复习所用)
考虑最小末端点,例如示例1中,最小末端点在A[0],那么因为两个列表中区间都不相交的缘故,A[0]只可能与B中的一个区间B[0]相交(否则B中区间会相交),那么A[0]在后续不可能与其他区间相交,则可以不再考虑(删去),此时就需要寻找下一个最小末端点,来继续上诉步骤。
为什么下一个最小末端点不是B[0]呢,因为可能存在下面这种情况:
因此代码如下:
class Solution:
def intervalIntersection(self, firstList: List[List[int]], secondList: List[List[int]]) -> List[List[int]]:
ret=[]
i=j=0 #双指针指向当前比较区间
while i<len(firstList) and j<len(secondList):
#找相交区间
start=max(firstList[i][0],secondList[j][0])
end=min(firstList[i][1],secondList[j][1])
if start<=end:
ret.append([start,end])
#找最小末端点存在的区间
if firstList[i][1]<secondList[j][1]:
i+=1
else:
j+=1
return ret
边栏推荐
- 【222】内存溢出及定位
- Dameng database_ Logical backup
- IP笔记(12)
- Calculation steps of principal component analysis
- ue4 瞄准偏移
- IA笔记 1
- Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
- Hololens 2 development 101: create the first hololens 2 Application
- 10大漏洞评估和渗透测试工具
- 使用Keras实现CNN+BiLSTM+Attention的多维(多变量)时间序列预测
猜你喜欢

Openpose Unity 插件部署教程

Calculation steps of principal component analysis

Do not rent servers, build your own personal business website (1)

ue4换装系统 1.换装系统的基本原理

不租服务器,自建个人商业网站(2)

How does the latest version of text (TMP) UI text of unity display in Chinese

IP job (1)

不租服务器,自建个人商业网站(4)

IA课总结(1)

IP class notes (4)
随机推荐
Dameng database_ Logical backup
IP笔记(9)
IP job (1)
Find the ArrayList < double > with the most occurrences in ArrayList < ArrayList < double >
Solve modularnotfounderror: no module named "cv2.aruco“
IP笔记(10)
不租服务器,自建个人商业网站(如何购买域名)
++cnt1[s1.charAt(i) - ‘a‘];
IP笔记(6)
Do not rent servers, build your own personal business website (3)
Public access intranet IIS website server [no public IP required]
Positional argument after keyword argument
Remote connection to Qunhui NAS at home [no public IP, free intranet penetration]
记一次高校学生账户密码的获取,从无到有
IA课总结(2)
Dameng database_ Common initialization parameters
leetcode 不用加减乘除算加法 || 二进制中1的个数
Dameng database_ Summary of supported data types
Simple but easy to use: using keras 2 to realize multi-dimensional time series prediction based on LSTM
Kernel pwn 基础教程之 Heap Overflow