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的更多相关文章

  1. [Swift]LeetCode986. 区间列表的交集 | Interval List Intersections

    Given two lists of closed intervals, each list of intervals is pairwise disjoint and in sorted order ...

随机推荐

  1. Dubbo(6)Dubbo服务集群实现负载均衡

    什么时候用到集群?比如说某个服务,并发量特别大的时候就会用到集群: 具体的话比如说,某些特殊的项目一天的注册量10万,国家的一些考试系统,集中在几天,注册量一天10万,如果只弄一个服务,根本服务不过来 ...

  2. erlang 安装

    [root@Aliyun software]# rpm -Uvh erlang-solutions-1.0.1.noarch.rpm [root@Aliyun software]# yum -y in ...

  3. 学习笔记之C / C++

    面试总结之C/C++ - 浩然119 - 博客园 https://www.cnblogs.com/pegasus923/p/5558919.html 学习笔记之C++ How to Program(p ...

  4. Centos7.2/7.3集群安装Kubernetes 1.8.4 + Dashboard(转)

    原文https://www.cnblogs.com/burningTheStar/p/7865998.html 1.环境配置 结点数量:3 结点系统:CentOS 7.2 / 7.3 2.效果展示 3 ...

  5. Notepad++ 列操作

    在网上找到一篇关于socket编程的文章,想把其中的代码直接拷贝下来运行测试,但是人家网站做的不够人性化,每行的开头都有行号,直接拷贝就要一行行的删除,甚是麻烦,想到linux下的vi编辑器可以完成列 ...

  6. docker之小记一

    PaaS上build新版本的基础组件的镜像总是失败,提示也不是很明确.突然想起来,镜像的依赖关系做过变更,可能是缺少基础镜像的原因. 由于没有统一的仓库或者只是我还不知道,就从制品库下载对应的镜像,然 ...

  7. vultr 上实现高可用冗余浮动公网IP出口(使用BIRD+BGP协议)High Availability on Vultr with Floating IP and BGP

    官方文档: https://www.vultr.com/docs/high-availability-on-vultr-with-floating-ip-and-bgp https://www.vul ...

  8. Golang值传递和指针传递

    Golang值传递和指针传递 package main import ( "fmt" ) func swap1(x, y, p *int) { if *x > *y { *x ...

  9. 跨域验证cookie与缓存控制

    1. 是否能跨域完全取决于浏览器控制,浏览器可以直接拒绝发送跨域请求(服务器根本收不到),也可以发送给服务器等接收到返回信息后决定是否让它被读取. 2. 服务器并不能辨别请求是从哪个源发过来的,只有在 ...

  10. CentOS6.6下安装VMware Tools

    摘要:为了方便虚拟机和主机之间复制粘贴文件,拖拽文件,需要安装VMwareTools.下面将我的安装步骤记录如下: 第一步:打开虚拟机后,在VM的工具栏中点虚拟机,安装VMwareTools(T).. ...