LeetCode-矩形重叠
题目描述:
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。
如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。
给出两个矩形,判断它们是否重叠并返回结果。
示例:
输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
输出:true
输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
输出:false
解题思路:
第一种(考虑边界值):
思路:我们可以在平面建一个坐标系,画出 rec2 的矩形,坐标分别为(rec2[0], rec2[1], rec2[2], rec2[3]),现在我们考虑不重叠情况。当 rec1 和 rec2 不发生重叠时,rec1 可以在 rec2 的 上、下、左、右。这里以左边为例(其他别思路相同):当 rec1 在 rec2 的左边时,rec1 右上角的 x 坐标(即:rec1[2])应该小于等于 rec2 的左下角的 x 坐标(即:rec2[0]), 表达式为:rec1[2] <= rec2[0]。其他方向思路相同。
rec1[2] <= rec2[0] # 左侧
rec1[0] >= rec2[2] # 右侧
rec1[1] >= rec2[3] # 上侧
rec1[3] <= rec2[1] # 下侧
代码:
from typing import List
def isRectangleOverlap(rec1: List[int], rec2: List[int]) -> bool:
return not (
rec1[2] <= rec2[0] or
rec1[0] >= rec2[2] or
rec1[1] >= rec2[3] or
rec1[3] <= rec2[1]
)
print(isRectangleOverlap(rec1 = [0,0,2,2], rec2 = [1,1,3,3])) # True
第二种(考虑重叠):
思路:画上一个平面坐标系,将 rec1 和 rec2 的矩形的部分面积相重叠,将两个矩形的 “左下角” “右上角” 的坐标投射到 x、y轴上,可以看到在 x 、y轴上,两者会有交集。由数学知识可得,当 min(rec1[2], rec2[2]) > max(rec1[0], rec2[0]) 时,在x轴上有交集,当 min(rec1[3], rec2[3]) > max(rec1[1], rec2[1]) 时,在y轴上右交集。

代码:
from typing import List def isRectangleOverlap(rec1: List[int], rec2: List[int]) -> bool:
return (min(rec1[2], rec2[2]) > max(rec1[0], rec2[0])
and min(rec1[3], rec2[3]) > max(rec1[1], rec2[1]))
print(isRectangleOverlap(rec1 = [0,0,2,2], rec2 = [1,1,3,3])) # True
LeetCode-矩形重叠的更多相关文章
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- leetcode 签到 836. 矩形重叠
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- LeetCode 836. 矩形重叠
题目链接:https://leetcode-cn.com/problems/rectangle-overlap/ 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左 ...
- Java实现 LeetCode 836 矩形重叠(暴力)
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- [Swift]LeetCode836. 矩形重叠 | Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- [LeetCode] 矩形面积
题目链接: https://leetcode-cn.com/problems/rectangle-area 难度:中等 通过率:41.3% 题目描述: 在 二维 平面上计算出两个 由直线构成的 矩形重 ...
- libgdx判断矩形重叠碰撞
有两种方式. 1. 排除法,排除四种不可能重叠的情况就是了. public static boolean IsOverlap( Rectangle rect1, Rectangle rect2 ){ ...
- hdu2056 矩形重叠面积(水题)
题意: 给你两个矩形,问你他们的重叠面积是多少. 思路: 这两个矩形是平行x和y轴的,所以水题,不解释. #include<stdio.h> typedef stru ...
- hdu1255 扫描线,矩形重叠面积(两次以上)
题意: 给你n个矩形,然后问你这n个矩形所组成的画面中被覆盖至少两次的面积有多大. 思路: 和1542差距并不是很大,大体上还是离散化+线段树扫面线,不同的地方就是这个题目要求 ...
- 218. The Skyline Problem *HARD* -- 矩形重叠
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- 吴裕雄--天生自然Android开发学习:android开发知识学习思维导图
- Nginx笔记:支持对用户提交URL和服务的URL不一致时,保持对POST提交的支持
用户访问的URL和服务的URL不一致,需要对URL修改,同时使用的是POST提交方式 location ~* ^/portalproxy/([-]*)/portal$ { #rewrite '^/po ...
- Nuxt.js 踩坑笔记 - 缓存向
零.前言 最近参与了一个立足 seo 的移动端项目,公司前端工程主栈 vue,所以理所当然的用上了 nuxt,UI 主要选择了 Vant. 一.公共列表页的缓存 公共列表页由于数据量较大,故需要滚 ...
- 浅谈Java转义符\\|
看一段程序 String t = "a||b||c||d"; String[] temp = t.split("\\|\\|"); System.out.pri ...
- Linux 下centos7启动 Tomcat 抛出Can't connect to X11 window server 问题的解决方法
1 问题 今天启动 Tomcat 后,登录页验证码不见了.在 localhost.xxx.log 发现以下错误: org.apache.catalina.core.StandardWrapperVal ...
- idea常见需求
1.给class加注释模板 /** *@ClassName ${NAME} *@Description TODO *@Author xxx *@Date ${DATE} ${TIME} *@Versi ...
- python socket粘包及实例
1.在linux中经常出现粘包的出现(因为两个send近靠着,造成接受到的数据是在一起的.)解决方法: 在服务端两send的中间中再添加一个recv(),客户端添加一个send(),服务端收到信息确认 ...
- curator配置及使用
1.action.yml --- actions: 1: action: index_settings options: index_settings: index: routing.allocati ...
- Newman+Jenkins实现接口自动化测试
目录 一.是什么Newman 二.如何安装 三.如何使用 1.运行本地文件 2.运行在线文件 3.以node.js库运行 4.导出报告 四.命令行测试真实接口 1.导出collection文件 2.导 ...
- 从头认识js-函数表达式
定义函数的方式有两种: 1.函数声明(特征:函数声明提升,在执行代码之前会先读取函数声明,这就意味着可以把函数声明放在调用它的语句之后) 2.函数表达式(函数表达式与其他表达式一样,使用之前必须先声明 ...