当前位置:网站首页>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);
};
边栏推荐
- @Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
- The harm of game unpacking and the importance of resource encryption
- Detailed explanation of heap sorting
- [NVIDIA development board] FAQ (updated from time to time)
- LeetCode:剑指 Offer 42. 连续子数组的最大和
- egg. JS getting started navigation: installation, use and learning
- LeetCode:236. 二叉树的最近公共祖先
- 【Nvidia开发板】常见问题集 (不定时更新)
- Process of obtaining the electronic version of academic qualifications of xuexin.com
- 堆排序详解
猜你喜欢
[embedded] print log using JLINK RTT
Sort according to a number in a string in a column of CSV file
Target detection - pytorch uses mobilenet series (V1, V2, V3) to build yolov4 target detection platform
Visual implementation and inspection of visdom
win10系统中的截图,win+prtSc保存位置
JS native implementation shuttle box
Simple use of promise in uniapp
深度剖析C语言数据在内存中的存储
【嵌入式】Cortex M4F DSP库
egg. JS project deployment online server
随机推荐
China dihydrolaurenol market forecast and investment strategy report (2022 Edition)
TCP/IP协议
Modify the video name from the name mapping relationship in the table
egg. JS project deployment online server
JVM performance tuning and practical basic theory - Part 1
JS pure function
【ROS】usb_ Cam camera calibration
移位运算符
[brush questions] top101 must be brushed in the interview of niuke.com
Excellent software testers have these abilities
R language ggplot2 visualization, custom ggplot2 visualization image legend background color of legend
View computer devices in LAN
【Nvidia开发板】常见问题集 (不定时更新)
延迟初始化和密封类
pytorch训练好的模型在加载和保存过程中的问题
Deep analysis of C language pointer
Restful API design specification
Deep analysis of C language data storage in memory
深度剖析C语言数据在内存中的存储
Simple use of promise in uniapp