leetcode986
class Solution:
def judgeIntersection(self,a:'Interval',b:'Interval'): #返回值1,bool类型,是否有交集:True-有交集,False-无交集
#返回值2,int类型,哪个先结束:0-A先结束,1-B先结束,2-AB同时结束
#返回值3,Interval类型,交集的值:有交集时是Interval实例,无交集时是None intersect = False firstEnd = 2
if a.end < b.end:
firstEnd = 0
elif a.end > b.end:
firstEnd = 1
else:
firstEnd = 2 intersectArea = Interval() if a.start <= b.start and a.end >= b.start and a.end <= b.end:
#print('True,[b.start,a.end]')
intersect = True
intersectArea.start=b.start
intersectArea.end=a.end
elif a.start >= b.start and a.start <= b.end and a.end >= b.end:
#print('True,[a.start,b.end]')
intersect = True
intersectArea.start=a.start
intersectArea.end=b.end
elif a.start>= b.start and a.end <= b.end:
#print('True,[a.start,a.end]')
intersect = True
intersectArea.start=a.start
intersectArea.end=a.end
elif a.start<=b.start and a.end >= b.end:
#print('True,[b.start,b.end]')
intersect = True
intersectArea.start=b.start
intersectArea.end=b.end
elif a.end < b.start:
#print('False,None')
intersect = False
intersectArea = None
elif a.start > b.end:
#print('False,None')
intersect = False
intersectArea = None
else:
print('error') return intersect,firstEnd,intersectArea def intervalIntersection(self, A: 'List[Interval]', B: 'List[Interval]') -> 'List[Interval]':
Aindex = 0
Bindex = 0
Alen = len(A)
Blen = len(B)
ListI = list() while Aindex < Alen and Bindex < Blen:
r1,r2,r3 = self.judgeIntersection(A[Aindex],B[Bindex])
if r1:
ListI.append(r3) if r2 == 0:
Aindex+=1
elif r2 == 1:
Bindex+=1
else:
Aindex+=1
Bindex+=1 return ListI
leetcode986的更多相关文章
- [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections
Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...
随机推荐
- csv文件操作
1.python2中: import csv infos = [ ['peter','male'], ['marry','female'], ['johon','male'], ['rose','fe ...
- 自动化 数据分离 --A文件里面的类 中的函数 调用 B文件里面类 的函数 的方法
记录: bb 要实例化 self.dr=dr,那么 iber_test类的 self.dr 才能带过去
- hadoop 伪分布式安装
0. 关闭防火墙 重启后失效 service iptables start ;#立即开启防火墙,但是重启后失效. service iptables stop ;#立即关闭防火墙,但是重启后失效. 重启 ...
- 廖雪峰Java1-3流程控制-4switch多重选择
switch语句 根据switch(表达式)跳转到匹配的case结果,继续执行case结果: 的后续语句,遇到break结束执行,没有匹配条件,执行default语句. int i = 3 switc ...
- 微信小程序调用微信登陆获取openid及用户信息 java做为服务端
转载的文章,很不错 https://blog.csdn.net/weilai_zhilu/article/details/77932630
- 文档碎片及xml讲解
1.将数据渲染到页面的几种方式 1.字符串拼接 2.dom循环 3.模板 4.文档碎片 字符串拼接: 优势:只进行一次dom回流 缺点:原有的dom事件会消失 案例分析:原有list中有3个li,并且 ...
- 反射中的BindingFlags
不指定绑定标志 BindingFlags.Default 表示忽略 name 的大小写,不应考虑成员名的大小写 BindingFlags.IgnoreCase 只应考虑在所提供类型的层次结构级别 ...
- Service 和 IntentService的区别;
Srevice不是在子线程,在Srevice中做耗时操作一样ANR,然后我们就会用到IntentService,IntentSrevice不但擅长做耗时操作,还有一个特点,用完即走: 在Srevice ...
- HTTP请求返回值所代表的含义
一些常见的状态码为: 200 - 服务器成功返回网页(表示请求成功) 404 - 请求的网页不存在(可能是网络的问题,也可能是网页没办法访问不代表网页不存在) 503 - 服务器超时(服务器故障) 下 ...
- asp.net 中日期的格式化显示的方法
在Asp.net 中经常使用日期,在不同的场合,对日期的显示方式有不同的要求,为此,自己总结了一些日期格式化的方式,仅供学习参考使用: C#格式化日期时间 DateTime dt = DateTime ...