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 and rec2 are lists of 4 integers.
All coordinates in rectangles will be between -10^9 and 10^9.
分情况讨论, 讨论不可能相交的四种情况(横轴与横轴比较,纵轴与纵轴比较)
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
//bottom-left corner
int x1r1 = rec1[0];
int y1r1 = rec1[1];
//top-right corner
int x2r1 = rec1[2];
int y2r1 = rec1[3]; //bottom-left corner
int x1r2 = rec2[0];
int y1r2 = rec2[1];
//top-right corner
int x2r2 = rec2[2];
int y2r2 = rec2[3]; if(y2r1 <= y1r2 || y2r2 <= y1r1){
return false;
}
if(x2r2 <= x1r1 || x2r1 <= x1r2){
return false;
}
return true;
}
}
LeetCode - Rectangle Overlap的更多相关文章
- [LeetCode] Rectangle Overlap 矩形重叠
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- [LeetCode] Rectangle Area 矩形面积
Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...
- 【Leetcode_easy】836. Rectangle Overlap
problem 836. Rectangle Overlap solution: class Solution { public: bool isRectangleOverlap(vector< ...
- 836. Rectangle Overlap ——weekly contest 85
Rectangle Overlap A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coor ...
- #Leetcode# 836. Rectangle Overlap
https://leetcode.com/problems/rectangle-overlap/ A rectangle is represented as a list [x1, y1, x2, y ...
- 【LeetCode】836. Rectangle Overlap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...
- [LeetCode&Python] Problem 836. Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
- LeetCode算法题-Rectangle Overlap(Java实现)
这是悦乐书的第325次更新,第348篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第195题(顺位题号是836).矩形表示为数组[x1,y1,x2,y2],其中(x1,y ...
- [Swift]LeetCode836. 矩形重叠 | Rectangle Overlap
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...
随机推荐
- SpringMVC中文乱码的解决办法
中文乱码分类: (1)按照请求分类: GET请求乱码 POST请求乱码 (2)按照乱码位置分类 从前台传到后台的数据乱码(存储到数据库中的数据乱码) 从后台传到前台的数据乱码(显示在页面的数据乱码) ...
- C/C++知识补充(2) C/C++操作符/运算符的优先级 & 结合性
, 逗号操作符 for( i = 0, j = 0; i < 10; i++, j++ ) ... 从左到右 Precedence Operator Description Example ...
- DevExpress ASP.NET Bootstrap Controls v18.2新功能详解(一)
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Boot ...
- 设置checkBox不拦截焦点
android:clickable="false"android:focusableInTouchMode="false"android:focusable=& ...
- Linux 配置selenium + webdriver 环境
1.ubuntu 自带了python, 可以打开终端输入python 回车后如果显示版本信息就说明已经安装 2.安装安装python setup tools apt-get install pytho ...
- shiro学习笔记(四) ini配置以及加解密
INI配置 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全且真个应用只需要一个即可,因此Sh ...
- <Spark><Running on a Cluster>
Introduction 之前学习的时候都是通过使用spark-shell或者是在local模式运行spark 这边我们首先介绍Spark分布式应用的架构,然后讨论在分布式clusters中运行Spa ...
- DeployMan,发布文件的利器
利用Delphi开发app,有时候需要发布文件,如果文件少还不是问题,但文件多的情况下,IDE带的发布功能,就显得捉襟见肘,效率低下了. 通过Project-Deployment,打开发布窗口,如下图 ...
- DHCP服务配置
DHCP(Dynamic Host Configuration Protocol)动态主机配置协议 -->是由Internet工作任务小组设计开发的,专用于对TCP/IP网络中的计算机自定分配T ...
- Window下部署Maven Nexus
Nexus下载地址:https://www.sonatype.com/download-oss-sonatype 选择相应的版本下载后,本人下载的是nexus-2.12.0-01-bundle.zip ...