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 ...
随机推荐
- MOOC(6)- case之间存在依赖
方法1:这里只给出根据依赖字段去匹配响应结果中的值的函数,其他匹配依赖case,对依赖case发起请求见下面的方法2 方法2: from day_20200208_mooc.tools.do_exce ...
- Spring第一课:IOC控制反转,什么是反转,什么又是控制?
前言 学习Spring第一课,就是认识IOC控制反转,要了解它还真得花一些功夫.今天主要理解透彻它的真谛,而不仅限于表面. 上道小菜 public class BusinessService { pr ...
- Linux下文件 ~/.bashrc 和 ~/.bash_profile 和 /etc/bashrc 和 /etc/profile 的区别 | 用户登录后加载配置文件的顺序
转自 https://blog.csdn.net/secondjanuary/article/details/9206151 文件说明: /ect/profile 此文件为系统的每个用户设置环境信息, ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- spring学习笔记一:spring介绍
jar包下载地址:http://repo.spring.io/release/org/springframework/spring/ spring特点: 1.非侵入性 spring框架的API不会在业 ...
- basecalling|vector mark|Assembly的难题|
生物信息学 染色体可以据染色图谱判断染色体号码,1-22号染色体依次变短,它们影响机体发育,23号染色体决定性别.肿瘤是由于遗传密码变异造成的.因此,遗传密码的解读非常重要,但是因为遗传密码长度非常长 ...
- curl模拟
header('content-type:text/html;charset=utf-8');function curlPost($url,$data,$method){ $ch = curl_ini ...
- Zabbix 监控进程参考
1)zabbix自动发现占用内存最大top10进程并监控资源 http://blog.csdn.net/ybx13218464908/article/details/47819401
- Header函数和PHP_AUTH_USER做用户验证(转载)
php Header PHP_AUTH_USER PHP_AUTH_PW 用户验证 在php中,可以使用Header函数做一些有趣的事情,用户验证就是其中一个很有意思的功能.具体用法: Header( ...
- failed to load main-class manifest attribute(运行jar包出错)
原因描述:MANIFEST.MF文件中的Main-Class配置不正确或格式不正确 检查方式:以WinRarR的方式打开jar包,如图所示, 点击进入箭头所指的META-INF文件夹 将MAN ...