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 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:

  1. Both rectangles rec1 and rec2 are lists of 4 integers.

  2. All coordinates in rectangles will be between -10^9 and 10^9.
 1 class Solution {
2 public:
3 bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
4 vector<int> xx,yy;
5 xx.push_back(rec1[0]);
6 xx.push_back(rec1[2]);
7 xx.push_back(rec2[0]);
8 xx.push_back(rec2[2]);
9 yy.push_back(rec1[1]);
10 yy.push_back(rec1[3]);
11 yy.push_back(rec2[1]);
12 yy.push_back(rec2[3]);
13 std::sort(xx.begin(),xx.end());
14 std::sort(yy.begin(),yy.end());
15 for(int x = 0; x < 4;x++){
16 for(int y = 0;y < 4;y++){
17 if(2*xx[x]+1 < 2*rec2[2]&&2*xx[x]+1 > 2*rec2[0]&&2*yy[y]+1 > 2*rec2[1]&&2*yy[y]+1 < 2*rec2[3]){
18 if(2*xx[x]+1 < 2*rec1[2]&&2*xx[x]+1 > 2*rec1[0]&&2*yy[y]+1 > 2*rec1[1]&&2*yy[y]+1 < 2*rec1[3]){
19 return true;
20 }
21 }
22 }
23 }
24 return false;
25 }
26 };

A easier solution:

1 bool isRectangleOverlap(vector<int>& a, vector<int>& b) {
2 return !(a[2] <= b[0] || b[2] <= a[0] || a[3] <= b[1] || b[3] <= a[1]);
3 }

836. Rectangle Overlap ——weekly contest 85的更多相关文章

  1. 【Leetcode_easy】836. Rectangle Overlap

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

  2. #Leetcode# 836. Rectangle Overlap

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

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

  4. 836. Rectangle Overlap 矩形重叠

    [抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...

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

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

  6. 835. Image Overlap —— weekly contest 84

    Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...

  7. 836. Rectangle Overlap

    class Solution { public: bool isRectangleOverlap(vector<int>& rec1, vector<int>& ...

  8. 838. Push Dominoes —— weekly contest 85

    Push Dominoes There are N dominoes in a line, and we place each domino vertically upright. In the be ...

  9. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

随机推荐

  1. VPS教程:搭建个人网盘教程—kodexplorer

    kodexplorer网盘系统.Kodexplorer,也叫芒果云.可道云.kodcloud,总之名字改了不少.但其本身作为一个网盘文件系统,还是有很多可圈可点的地方. seafile.h5ai.ko ...

  2. JavaFX FileChooser文件选择器,缓存上一次打开的目录

    例1:点击按钮Choose File打开文件选择器,并打开指定的目录.这是通过final void setInitialDirectory(final File value)方法实现的. 1 impo ...

  3. 数据结构与算法:AVL树

    AVL树 在计算机科学中,AVL树是最先发明的自平衡二叉查找树.在AVL树中任何节点的两个子树的高度最大差别为1,所以它也被称为高度平衡树.增加和删除可能需要通过一次或多次树旋转来重新平衡这个树.AV ...

  4. 发布MeteoInfo Java 1.2.1

    主要增加了合并netCDF文件的功能.在不同时间netCDF文件合并时考虑了不同文件起始时间不同的情况.

  5. pytest文档42-fixture参数化params

    前言 参数化是自动化测试里面必须掌握的一个知识点,用过 unittest 框架的小伙伴都知道使用 ddt 来实现测试用例的参数化. pytest 测试用例里面对应的参数可以用 parametrize ...

  6. 拦截导弹简单版——线性dp

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  7. centos7安装redis6

    wget http://download.redis.io/releases/redis-6.0.6.tar.gz?_ga=2.104030464.1049731693.1595900008-1330 ...

  8. centos8安装及配置nfs4

    一,用rpm检查是否有nfs-utils的包已安装 [root@localhost liuhongdi]# rpm -qa | grep nfs-utils nfs-utils-2.3.3-26.el ...

  9. 第四章 NFS服务相关介绍

    一.NFS服务介绍 1.什么是NFS?是一个共享存储,文件服务器 2.NFS基本概述NFS是Network File System的缩写及网络文件系统.NFS主要功能是通过局域网络让不同的主机系统之间 ...

  10. 跟我一起学.NetCore之MVC过滤器,这篇看完走路可以仰着头走

    前言 MVC过滤器在之前Asp.Net的时候就已经广泛使用啦,不管是面试还是工作,总有一个考点或是需求涉及到,可以毫不疑问的说,这个技术点是非常重要的: 在之前参与的面试中,得知很多小伙伴只知道有一两 ...