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 ...
随机推荐
- 2015-09-15-git配置
https://help.github.com/articles/set-up-git/ git上传是忽略一些文件 在每个git的项目中有一个.gitignore文件,将忽略的文件或文件夹输入即可. ...
- 用两个栈实现一个队列(C++)
分析 栈:后进先出 队列:先进先出 要使用两个栈实现队列(先进先出),主要思路是 1.插入一个元素:直接将元素插入stack1即可. 2.删除一个元素:当stack2不为空时 ,直接弹出栈顶元素,当s ...
- 配置gitlab(备忘)
已经配置好github的基础上,clone gitlab 地址git status 显示改变了的文件但是webstorm文件颜色不改变问题的解决:VCS->git-->remotes--& ...
- mysql视图、事务、触发器、索引
视图 什么是视图 ? 一个查询语句的结果是一张虚拟表,将这种虚拟表保存下来,它就变成了一个视图. 为什么要用视图? 当频繁需要用到多张表的连表结果,你就可以事先生成好视图,之后直接调用即可,避免了反复 ...
- Python---10小结
因一边上班一边自学python,一旦忙起来,python就会放两天,可是2天后之前学的内容就会有点忘记. 今天把python的各种启动方法总结一下; 我的文档路径: ------- 1打开文件所在的c ...
- Annotation标注
# View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...
- 从Instagram“宁静、规则”的成功 看国内APP发展之路
看国内APP发展之路" title="从Instagram"宁静.规则"的成功 看国内APP发展之路"> Instagram在全球获得的巨大成功 ...
- Spring源码分析-BeanFactoryPostProcessors 应用之 PropertyPlaceholderConfigurer
BeanFactoryPostProcessors 介绍 BeanFactoryPostProcessors完整定义: /** * Allows for custom modification of ...
- SpringCloud入门(六): Hystrix监控
Hystrix.stream 监控 <!--. 配置pom文件,引入actuator包--> <dependency> <groupId>org.springfra ...
- golang在debian下不能用sudo进行使用的问题
sudo ln -s /usr/local/go/bin/go /usr/bin/go 然后就ok了. 去查了下这两个路径的差别,也没查出什么.只是说/usr/bin 是系统预装所在的路径.