当前位置:网站首页>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);
};
边栏推荐
- [brush questions] top101 must be brushed in the interview of niuke.com
- Deep analysis of C language pointer
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
- China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
- Cisp-pte practice explanation
- 电脑清理,删除的系统文件
- 堆排序详解
- JS native implementation shuttle box
- The problem and possible causes of the robot's instantaneous return to the origin of the world coordinate during rviz simulation
猜你喜欢
Deep analysis of C language data storage in memory
同一局域网的手机和电脑相互访问,IIS设置
可变长参数
Roguelike game into crack the hardest hit areas, how to break the bureau?
Precise query of tree tree
Simple use of promise in uniapp
Sublime text using ctrl+b to run another program without closing other runs
704 binary search
marathon-envs项目环境配置(强化学习模仿参考动作)
JVM quick start
随机推荐
有效提高软件产品质量,就找第三方软件测评机构
Current situation and trend of character animation
JS inheritance method
JVM performance tuning and practical basic theory - Part 1
Revit secondary development Hof method calls transaction
ESP8266-RTOS物联网开发
移位运算符
Problems in loading and saving pytorch trained models
Delay initialization and sealing classes
Promise 在uniapp的简单使用
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
生成器参数传入参数
优秀的软件测试人员,都具备这些能力
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Roguelike游戏成破解重灾区,如何破局?
What is the role of automated testing frameworks? Shanghai professional third-party software testing company Amway
[MySQL] lock
Purpose of computer F1-F12
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
hutool优雅解析URL链接并获取参数