当前位置:网站首页>LeetCode:836. 矩形重叠
LeetCode:836. 矩形重叠
2022-07-06 08:44:00 【Bertil】
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。矩形的上下边平行于 x 轴,左右边平行于 y 轴。
如果相交的面积为 正 ,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。
给出两个矩形 rec1 和 rec2 。如果它们重叠,返回 true;否则,返回 false 。
示例 1:
输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
输出:true
示例 2:
输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
输出:false
示例 3:
输入:rec1 = [0,0,1,1], rec2 = [2,2,3,3]
输出:false
提示:
rect1.length == 4
rect2.length == 4
-10^9 <= rec1[i], rec2[i] <= 10^9
rec1 和 rec2 表示一个面积不为零的有效矩形
解题思路
1.首先找出不重叠的四种情况,以两图形左下角和右上角的横纵坐标进行表示
2。然后返回这四种情况的反运算结果即可
代码
/** * @param {number[]} rec1 * @param {number[]} rec2 * @return {boolean} */
var isRectangleOverlap = function(rec1, rec2) {
const [x1, y1, x2, y2] = rec1;
const [x3, y3, x4, y4] = rec2;
return !(x1 >= x4 || x3 >= x2 || y3 >= y2 || y1 >= y4);
};
边栏推荐
- MySQL learning record 07 index (simple understanding)
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- ROS编译 调用第三方动态库(xxx.so)
- sublime text的编写程序时的Tab和空格缩进问题
- ROS compilation calls the third-party dynamic library (xxx.so)
- R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
- Promise 在uniapp的简单使用
- Verrouillage [MySQL]
- 如何有效地进行自动化测试?
- To effectively improve the quality of software products, find a third-party software evaluation organization
猜你喜欢
Unsupported operation exception
Navicat Premium 创建MySql 创建存储过程
Problems in loading and saving pytorch trained models
ESP8266-RTOS物联网开发
Navicat premium create MySQL create stored procedure
Crash problem of Chrome browser
个人电脑好用必备软件(使用过)
被破解毁掉的国产游戏之光
View computer devices in LAN
Marathon envs project environment configuration (strengthen learning and imitate reference actions)
随机推荐
The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
超高效!Swagger-Yapi的秘密
Light of domestic games destroyed by cracking
How to effectively conduct automated testing?
ROS编译 调用第三方动态库(xxx.so)
Charging interface docking tutorial of enterprise and micro service provider platform
Excellent software testers have these abilities
电脑清理,删除的系统文件
What is CSRF (Cross Site Request Forgery)?
sublime text的编写程序时的Tab和空格缩进问题
MySQL learning record 10getting started with JDBC
Swagger setting field required is mandatory
移位运算符
ROS compilation calls the third-party dynamic library (xxx.so)
Bottom up - physical layer
企微服务商平台收费接口对接教程
China vanadium battery Market Research and future prospects report (2022 Edition)
[NVIDIA development board] FAQ (updated from time to time)
visdom可视化实现与检查介绍
Modify the video name from the name mapping relationship in the table