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 ...
随机推荐
- KeyBoardEvent
顺便提一句 在纯Flash CS环境下初始要这样写stage?init(null):addEventListener (Event.ADDED_TO_STAGE, init);即 if(stage ! ...
- 【剑指offer】调整数组数字位置
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. *思路:遍历数组,找到第一个偶 ...
- ASP.NET重写Render 加载CSS样式文件和JS文件(切换CSS换皮肤)
网页换皮肤的方式有很多种,最简单的通常就是切换页面CSS,而CSS通常写在外部CSS文件里.那么切换CSS其实就是更换html里的link href路径.我在网上搜索了下. 一般有两种方式: 1.页面 ...
- Visual Studio 2010 Shortcut
General Shortcut Description Ctrl-X or Shift-Delete Cuts the currently selected item to the clipboar ...
- 阿里云服务器 ECS Linux操作系统加固
1. 账号和口令 1.1 禁用或删除无用账号 减少系统无用账号,降低安全风险. 操作步骤 使用命令 userdel <用户名> 删除不必要的账号. 使用命令 passwd -l <用 ...
- 1121 Damn Single (25 分)
1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...
- 几个简单常用的jQuery实例
一.点赞效果: 1.1 效果: 1.2 代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 利用队列Queue实现一个多并发“线程池”效果的Socket程序
本例通过利用类Queue建立了一个存放着Thread对象的“容器对象”,当Client端申请与Server端通信时,在Server端的“链接循环”中每次拿出一个Thread对象去创建“线程链接”,从而 ...
- intent--Activity之间数据传递之Intent数据传递
intent传值: 4,intent传集合 3,intent传对象, 2,传递后有返回值的情况:当需要从目标Activity回传数据到原Activity时,可以使用上述方法定义一个新的Intent来传 ...
- web项目中添加MySQL驱动
1.我这里采用yml文件来配置,yml有配置层次清晰,方便操作的好处: 将application.properties后缀改成yml,即配置文件变成application.yml 我的applicat ...