[LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2]
, where (x1, y1)
are the coordinates of its bottom-left corner, and (x2, y2)
are the coordinates of its top-right corner.
Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.
Given two (axis-aligned) rectangles, return whether they overlap.
Example 1:
Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true
Example 2:
Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false
Notes:
- Both rectangles
rec1
andrec2
are lists of 4 integers. - All coordinates in rectangles will be between
-10^9
and10^9
.
这道题让我们求两个矩形是否是重叠,矩形的表示方法是用两个点,左下和右上点来定位的。下面的讲解是参见网友大神jayesch的帖子来的,首先,返璞归真,在玩 2D 之前,先看下 1D 上是如何运作的。对于两条线段,它们相交的话可以是如下情况:
x3 x4
|--------------|
|--------------|
x1 x2
我们可以直观的看出一些关系:
x1 < x3 < x2 && x3 < x2 < x4
可以稍微化简一下:
x1 < x4 && x3 < x2
就算是调换个位置:
x1 x2
|--------------|
|--------------|
x3 x4
还是能得到同样的关系:
x3 < x2 && x1 < x4
好,下面我们进军 2D 的世界,实际上 2D 的重叠就是两个方向都同时满足 1D 的重叠条件即可。由于题目中说明了两个矩形的重合面积为正才算 overlap,就是说挨着边的不算重叠,那么两个矩形重叠主要有这四种情况:
1)两个矩形在矩形1的右上角重叠:
____________________x4,y4
| |
_______|______x2,y2 |
| |______|____________|
| x3,y3 |
|______________|
x1,y1
满足的条件为:x1 < x4 && x3 < x2 && y1 < y4 && y3 < y2
2)两个矩形在矩形1的左上角重叠:
___________________ x4,y4
| |
| _______|____________x2,y2
|___________|_______| |
x3,y3 | |
|___________________|
x1,y1
满足的条件为:x3 < x2 && x1 < x4 && y1 < y4 && y3 < y2
3)两个矩形在矩形1的左下角重叠:
____________________x2,y2
| |
_______|______x4,y4 |
| |______|____________|
| x1,y1 |
|______________|
x3,y3
满足的条件为:x3 < x2 && x1 < x4 && y3 < y2 && y1 < y4
4)两个矩形在矩形1的右下角重叠:
___________________ x2,y2
| |
| _______|____________x4,y4
|___________|_______| |
x1,y1 | |
|___________________|
x3,y3
满足的条件为:x1 < x4 && x3 < x2 && y3 < y2 && y1 < y4
仔细观察可以发现,上面四种情况的满足条件其实都是相同的,只不过顺序调换了位置,所以我们只要一行就可以解决问题了,碉堡了。。。
class Solution {
public:
bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
return rec1[] < rec2[] && rec2[] < rec1[] && rec1[] < rec2[] && rec2[] < rec1[];
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/836
类似题目:
参考资料:
https://leetcode.com/problems/rectangle-overlap/
https://leetcode.com/problems/rectangle-overlap/discuss/132319/My-One-Line-C%2B%2B-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Rectangle Overlap 矩形重叠的更多相关文章
- 836. Rectangle Overlap 矩形重叠
[抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- leetcode 签到 836. 矩形重叠
836. 矩形重叠 矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的 ...
- Leetcode836.Rectangle Overlap矩阵重叠
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标. 如果相交的面积为正,则称两矩形重叠.需要明确的是,只在角或边接触的 ...
- [LeetCode] Image Overlap 图像重叠
Two images A and B are given, represented as binary, square matrices of the same size. (A binary ma ...
- LeetCode - Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- [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] 223. Rectangle Area 矩形面积
Find the total area covered by two rectilinearrectangles in a 2D plane. Each rectangle is defined by ...
- LeetCode算法题-Rectangle Overlap(Java实现)
这是悦乐书的第325次更新,第348篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第195题(顺位题号是836).矩形表示为数组[x1,y1,x2,y2],其中(x1,y ...
随机推荐
- Java基础 -- Java 抽象类 抽象方法
总结: 1. 抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过.只有抽象类的非抽象子类可以创建对象. 2. 抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类. ...
- css奇技淫巧-色彩渐变与动态渐变
来源 css渐变 CSS 中设置的渐变是 gradient 数据类型,它是一种特别的image数据类型.使用background-image设置,可叠加设置多个: CSS3 定义了两种类型的渐变(gr ...
- 小程序:navigateBack()修改数据
1.获取当前页面js里面的pages里的所有信息var pages = getCurrentPages(); 2. -2上一个页面 -3是上上个页面 var prevPage = pages[p ...
- The Apache Tomcat installation at this directory is version 8.5.40. A Tomcat 8.0 installation is expected.
问题描述 Eclipse 配置 Apache Tomcat 8.5.40(8.0.x 以上版本),会报如下错误信息: 解决方法 1)在 Apache Tomcat 的安装目录中找到 lib 目录下的 ...
- element-ui对话框组件Dialog在回调事件opened获取组件滚动条scrollTop的问题
今天使用element中的Dialog组件时发现一个问题:当Dialog内容过多时会出现滚动条,而当你滚动到一定位置后关闭Dialog,然后再次打开时滚动条仍然保持在上一次关闭前的位置而没有回到顶部. ...
- php配置文件参数设置
pm.max_children 设置多大合适? 一.pm.max_children 多大合适? 这个值原则上是越大越好,php-cgi的进程多了就会处理的很快,排队的请求就会很少. 设置”max_ch ...
- ubuntu快速部署gitlab汉化容器
前言:gitlab的原理我就不扯了(看这个https://www.jianshu.com/p/567207ac51cd),下面直接上操作 1.前提: a.要有docker的运行环境,用service ...
- [转] 以 async/await 为例,说明 babel 插件怎么搭
你一定碰到过这些库 babel-polyfill 项目地址:https://github.com/babel/babel/blob/master/packages/babel-polyfill 通过两 ...
- JAVA-MyBaits对应XML的两种使用方式
概述 在使用XML写SQL语句的时候,遇到参数传递的两种方式,也就是Mapper里面带@Param注解和不带的情况,容易混淆,对应XML的写法也不相同,使用的时候要注意对照代码比对(备注XML里面的关 ...
- 简易promise的实现(二)
code 上一章中我们遇到了两个问题 1.异步调用顺序的问题 2.then返回一个promise的问题 思考 如果控制异步回调的顺序? 因为异步操的时间作我们无法控制,但是我们只需要按顺序执行回调函数 ...