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. 深入理解Connector

    上一篇主要是从各个容器的生命周期的角度讲了一下整个tomcat的运行流程,说明了各个容器之间的调用关系.但并没有太过详细的说明每一个组件并区分他们. 下面从功能的角度上详细的分析一下connector ...

  2. 准备下上机考试,各种排序!!以后再添加和仿真像wiki上那样!

    #include <stdio.h> #include <string.h> #define N 6 typedef struct { ]; int score; }stude ...

  3. Cracking The Coding Interview 9.1

    //原文: // // You are given two sorted arrays, A and B, and A has a large enough buffer at the end to ...

  4. Python返回值不同格式的取值方式

    例: { "success": true, "topic_id": "5c89021773798770589936b0"} 转换成text, ...

  5. CentOS7安装cratedb

    crate: 下载: https://crate.io/download/thank-you/?download=tar crash: 下载: https://crate.io/docs/client ...

  6. Android:E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f9d1b41c0

    这个问题是在测试leakCanaryTestDemo时发现的,期初看到有点蒙,这个demo中只使用了一个button和一个textView控件进行测试,按理说是不应该出现这种问题,在 网上查找这个问题 ...

  7. Strict Standards: Declaration of UserModel::toJSON() should be compatible with that of BaseModel::toJSON()

    使用php报了这个错误: 错误的意思是:  严格标准: usermodel中的 toJSON() 方法 应该 同 BaseModel中的toJson() 方法是兼容的. php要求 子类的方法如果同父 ...

  8. FCC JS基础算法题(8):Slasher Flick(截断数组)

    题目描述: 返回一个数组被截断n个元素后还剩余的元素,截断从索引0开始. 这个题目有两个方法,都比较简单,用slice方法: function slasher(arr, howMany) { // 请 ...

  9. 64-65管道,rm与rmdir

    听说linux根目录tmp文件夹内是临时文件.用久了会产生很多垃圾文件 请问下面的临时文件怎么维护清理?全部没用删除,还是? /tmp 公用的临时文件存储点 linux下的文件结构 /bin 二进制可 ...

  10. HDU 6059 17多校3 Kanade's trio(字典树)

    Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy ...