leetcode 签到 836. 矩形重叠

836. 矩形重叠

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。

如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。

给出两个矩形,判断它们是否重叠并返回结果。

示例 1:

输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
输出:true
示例 2:

输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
输出:false
提示:
两个矩形 rec1 和 rec2 都以含有四个整数的列表的形式给出。
矩形中的所有坐标都处于 -10^9 和 10^9 之间。
x 轴默认指向右,y 轴默认指向上。
你可以仅考虑矩形是正放的情况。
来源:leetcode
链接:https://leetcode-cn.com/problems/rectangle-overlap/

思路:

假设
rec1:[0,0,2,2]->[x10,y11,x12,y13]
(x10即rec1的第1位)
rec2:[1,1,3,3]->[x20,y21,x22,y23]
列举出不匹配的情况:
x轴:x22<=x10或者x12<=x20
y轴:y11<=y23或者y21<=y13
代码

class Solution:
    def isRectangleOverlap(self, rec1, rec2) -> bool:
        if rec1[1]>=rec2[3] or(rec1[0]>=rec2[2] or rec2[0]>=rec1[2]):
            return False
        elif rec2[1]>=rec1[3] or (rec1[0]>=rec2[2] or rec2[0]>=rec1[2]):
            return False
        else:
            return  True
a=Solution()
rec1 = [0,0,2,2]
rec2 = [1,1,3,3]
print(a.isRectangleOverlap(rec1,rec2))

原文地址:https://www.cnblogs.com/rmxob/p/12516736.html

时间: 2024-11-09 03:50:56

leetcode 签到 836. 矩形重叠的相关文章

Leetcode 836. 矩形重叠

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩形 rec

836. 矩形重叠

题目: 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩形

[LeetCode] Rectangle Overlap 矩形重叠

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b

LeetCode 29. 矩形重叠 反向思维

题目描述 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们是否重叠并返回结果. 示例 1: 输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3] 输出:true 示例 2: 输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1] 输出:false 提示: 两个矩

矩形重叠检测。

// 矩形重叠类型注释 // CORNER_OVERLAP // -------------------- // | | // | | // | **********|********** // | * | * // | * | * // ---------*---------| * // * * // * * // ********************* // // ANCHOR_OVERLAP // ---- // | | // | | // *******|**|******* //

[Swift]LeetCode836. 矩形重叠 | Rectangle Overlap

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.  To b

矩形重叠

此博客链接:https://www.cnblogs.com/ping2yingshi/p/12517261.html 矩形重叠(43min) 题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的两个矩形不构成重叠. 给出两个矩形,判断它们

836. Rectangle Overlap 矩形重叠

[抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner. Two rectangles overlap if the area of their intersection is positive.

[LeetCode] Rectangle Area 矩形面积

Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. Cre