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 ...
随机推荐
- Django+Xadmin+Echarts动态获取数据legend颜色显示灰色问题已解决
前段时间做的使用Django的Xadmin后台和百度Echarts进行后台数据可视化,功能虽然实现,展示出来的legend图例,都是灰色的,只有鼠标放上去才会显示彩色的.百度都快被我刨穿了,看到有类似 ...
- windows下的pycharm配置 linux环境
由于最近学习python的需要,为了方便程序的调试,尝试在Windows下的Pycharm远程连接到虚拟机中Centos下的python环境.(这里我采用的是ssh的远程连接)1.准备工作: 固定ce ...
- [javamail]AUTH LOGIN failed;Invalid username or password报错
项目中需要用到javamailAPI,邮箱服务器用的sohu闪电邮,SMTP协议用来发送,赋值代码: Properties props = new Properties(); props.setPro ...
- 【转载】 unity 塔防游戏
原文地址: Part1的地址:http://www.cnblogs.com/lcxBlog/p/6075984.html Part2的地址:http://www.cnblogs.com/lcxBlog ...
- 锤子科技 Smartisan M1L 咖啡金 真皮背面 高配版 5.7
http://www.smartisan.com/m1/#/os 快人一步的OS http://www.smartisan.com/shop/#/buyphone?c=coffee&v= ...
- 网页设置下载apk
APK文件其实是zip格式,但后缀名被修改为apk,通过UnZip解压后,可以看到Dex文件,Dex是Dalvik VM executes的全称,即Android Dalvik执行程序,并非Ja ...
- בוא--来吧--IPA--希伯来语
灰常好听的希伯来语歌曲, Rita唱得真好.
- [Leetcode 62]机器人走路Unique Path 动态规划
[题目] A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below) ...
- Cracking The Coding Interview 5.6
//Write a program to swap odd and even bits in an integer with as few instructions as possible (e.g. ...
- 深入理解java虚拟机---垃圾回收(十一)
1.垃圾回收要解决的问题 可以通过配置虚拟机参数来打印出内存日志: -verbose:gc -XX:+PrintGCDetails 垃圾收集(Garbage Collection,GC),要设计一个G ...