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))

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

  1. Java实现 LeetCode 836 矩形重叠(暴力)

    836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...

  2. LeetCode 836. 矩形重叠

    题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...

  3. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

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

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  5. 836. Rectangle Overlap 矩形重叠

    [抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...

  6. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  7. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  8. [LeetCode] Image Overlap 图像重叠

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

  9. libgdx判断矩形重叠碰撞

    有两种方式. 1. 排除法,排除四种不可能重叠的情况就是了. public static boolean IsOverlap( Rectangle rect1, Rectangle rect2 ){ ...

随机推荐

  1. GCD学习 —— 三

    ​ 学习学习dispatch_block,在向队列中添加任务时,可以直接在对应的函数中添加 block.但是如果想对任务进行操作,比如监听任务.取消任务,就需要获取对应的 block. 1 创建Blo ...

  2. Android API Levels

    Android API Levels(转) 在本文中 API的级别是什么? 在Android中使用API级别 开发者需要考虑的内容 应用程序的向前兼容性 应用程序的向后兼容性 平台版本和API级别的选 ...

  3. 用hugo建博客的记录 · 老张不服老

    前后累计折腾近6个小时,总算把搭建hugo静态博客的整个过程搞清楚了.为什么用了这么久?主要还是想偷懒,不喜欢读英文说明.那就用中文记录一下过程吧.还是中文顺眼啊. 某日发现自己有展示些东西给外网的需 ...

  4. ADO.NET中DataTable类的使用

    DataTable类将关系数据表示为表格形式.在创建DataTable之前,必须包含System.Data名称空间.ADO.NET提供了一个DataTable类来独立创建和使用数据表.它也可以和Dat ...

  5. spring jpa ManyToMany 理解和使用

    1.java和jpa 中所有的关系都是单向的.这个关系数据库不同,关系数据库,通过外键定义并查询,使得反向查询总是存在的. 2.JPA还定义了一个OneToMany关系,它与ManyToMany关系类 ...

  6. Electron打包H5网页为桌面运行程序

    一.安装配置环境 Electron(一种桌面应用程序运行时),Electron 把 Chromium 和 Node 合并到一个单独的运行时里面,很适合开发桌面 web 形式的应用程序,通过Node它提 ...

  7. p2.js 与 createjs 的组合应用

    开始前简单说下其他几款js物理引擎 box2d老牌,功能全面,但是效率低下,移动端基不用考虑的 matterjs  效率目前我测试下来最高,但是依然还在开发中(好像还很缓慢),目前功能局限,而且有bu ...

  8. PHP 深度理解preg_quote()函数

    php手册上说,preg_quote()函数的作用是转义正则表达式字符.那么下面我们来深入了解下这个函数是怎么使用的: 说明:preg_quote()函数常和preg_replace()函数一起使用. ...

  9. SpringCloud第二代实战系列:一文搞定Nacos实现服务注册与发现

    一.背景:SpringCloud 生态圈 在正式开始本篇文章之前我们先岔开来讲一下SpringCloud的生态圈. SpringCloud大家都比较熟悉了,它制定了分布式系统的标准规范,做了高度抽象和 ...

  10. django 从零开始 11 根据时间戳加密数据

    django自带一个加密的方法signer,对数据进行一个加密 一般这种方式用于账号密码邮箱找回,或者token设置 class TimestampSigner(Signer): def timest ...