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的更多相关文章

  1. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  2. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  3. 【Leetcode_easy】836. Rectangle Overlap

    problem 836. Rectangle Overlap solution: class Solution { public: bool isRectangleOverlap(vector< ...

  4. 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 ...

  5. #Leetcode# 836. Rectangle Overlap

    https://leetcode.com/problems/rectangle-overlap/ A rectangle is represented as a list [x1, y1, x2, y ...

  6. 【LeetCode】836. Rectangle Overlap 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...

  7. [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 ...

  8. LeetCode算法题-Rectangle Overlap(Java实现)

    这是悦乐书的第325次更新,第348篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第195题(顺位题号是836).矩形表示为数组[x1,y1,x2,y2],其中(x1,y ...

  9. [Swift]LeetCode836. 矩形重叠 | Rectangle Overlap

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

随机推荐

  1. UVALive 3401 - Colored Cubes 旋转 难度: 1

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. 玩linux就是不断的踩坑,踩坑。最近的坑。xpath firefox兼容问题,抓取表格。

    最近在抓取一个页面表格时发现,用firefox提取的xpath,不能用,仔细分析后,发现是提取的xpath多了一个tbody标签.在xpath路径中删掉这段就好了. last_A5='/html/bo ...

  3. vue-12-渲染函数 & JSX

    render() Vue.component('anchored-heading', { render: function (createElement) { return createElement ...

  4. Cracking The Coding Interview 4.7_暂存

    //原文: // // You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  5. 7.9 C++ STL算法

    参考:http://www.weixueyuan.net/view/6406.html 总结: STL提供了大量操作容器的算法,这些算法大致可以分为:排序.搜索.集合运算.数值处理和拷贝等,这些算法的 ...

  6. 5.2 C++重载操作符的优先级

    参考:http://www.weixueyuan.net/view/6380.html 总结: 重载操作符不能改变操作符的优先级和语法特性. 重载操作符不能改变操作符的优先级和语法特性.例如上一节复数 ...

  7. DevExpress WPF v18.2新版亮点(三)

    买 DevExpress Universal Subscription  免费赠 万元汉化资源包1套! 限量15套!先到先得,送完即止!立即抢购>> 行业领先的.NET界面控件2018年第 ...

  8. Ubuntu16.04 安装Teamviewer

    有时需要远程控制ubuntu系统的电脑,Teamviewer在linux下也可以进行安装,大致看了下向日葵在linux下配置好像比较麻烦,而且Teamviewer远程控制的流畅性一直不错,就选择安装T ...

  9. 牛客多校第四场 F Beautiful Garden

    链接:https://www.nowcoder.com/acm/contest/142/F来源:牛客网 题目描述 There's a beautiful garden whose size is n ...

  10. adb shell按键操作(input keyevent)

    前言:input keyeven操作发送手机上常用的一些按键操作 一.keyevent事件对应数字 电话键 KEYCODE_CALL: 拨号键 KEYCODE_ENDCALL: 挂机键 KEYCODE ...