leetcode986

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

leetcode986. Interval List Intersections

leetcode986-区间列表的交集(c++/python)

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

Leetcode.1024 视频拼接

LeetCode刷题总结-双指针位运算和分治法篇

LeetCode810. 黑板异或游戏/455. 分发饼干/剑指Offer 53 - I. 在排序数组中查找数字 I/53 - II. 0~n-1中缺失的数字/54. 二叉搜索树的第k大节点(代码片段