836. Rectangle Overlap

题目

在这里插入图片描述

我的代码(效率较高)

class Solution(object):
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        if rec1[0]>=rec2[2]:
            return False
        if rec1[1]>=rec2[3]:
            return False
        
        if rec2[0]>=rec1[2]:
            return False
        if rec2[1]>=rec1[3]:
            return False
        else:
            return True

优秀代码

与上面代码类似。

class Solution(object):
    def isRectangleOverlap(self, rec1, rec2):
        """
        :type rec1: List[int]
        :type rec2: List[int]
        :rtype: bool
        """
        # method 1, 16ms, 100%; 10.7MB, %
        # 判斷是否重疊太麻煩, 太多種類;
        # 不如直接判斷是否沒重疊
        # 只要四個點都在外面就可以了
        # 總共只有四種情況...
        for idx, num in enumerate(rec2):
            if idx == 0:
                if rec1[2] <= num:
                    return False
            elif idx == 1:
                if rec1[3] <= num:
                    return False
            elif idx == 2:
                if rec1[0] >= num:
                    return False
            else:
                if rec1[1] >= num:
                    return False
        return True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值