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. 故意使用free掉的内存的一个实验( 常量区/栈)

    故意使用free掉的内存的一个实验 考虑一下两种声明 struct stuff{ char home[10]; int num; char name[10]; }; struct stuff{ cha ...

  2. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛

    今年的国庆档电影市场的表现还是比较强势的,两名主力<我和我的家乡>和<姜子牙>起到了很好的带头作用. <姜子牙>首日破 2 亿,一举刷新由<哪吒之魔童降世&g ...

  3. P3660 [USACO17FEB]Why Did the Cow Cross the Road III G

    Link 题意: 给定长度为 \(2N\) 的序列,\(1~N\) 各处现过 \(2\) 次,i第一次出现位置记为\(ai\),第二次记为\(bi\),求满足\(ai<aj<bi<b ...

  4. 14.深入k8s:kube-proxy ipvs及其源码分析

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 源码版本是1.19 这一篇是讲service,但是基础使用以及基本概念由于官方实在是写的 ...

  5. LiteOS-任务篇-源码分析-删除任务函数

    目录 前言 笔录草稿 源码分析 LOS_TaskDelete函数源码分析 完整源码 参考 链接 前言 20201009 LiteOS 2018 需要会通用链表 笔录草稿 源码分析 LOS_TaskDe ...

  6. Nuxt|Vue仿探探/陌陌卡片式滑动|vue仿Tinder拖拽翻牌效果

    探探/Tinder是一个很火的陌生人社交App,趁着国庆假期闲暇时间倒腾了个Nuxt.js项目,项目中有个模块模仿探探滑动切换界面效果.支持左右拖拽滑动like和no like及滑动回弹效果. 一览效 ...

  7. Scala小记(一)

    Scala小记----初识Scala 一,什么是Scale? Scala是一门面向对象的,使用JVM运行的函数式编程语言,(函数式编程语言:指的就是那些将方法或者说是函数来作为参数 进行传递的编程语言 ...

  8. 如何将python下载源地址修改为国内镜像源

    (1)在  C:\Users\xxx 下面创建新的目录  pip 文件夹 (2)在 pip目录下创建后缀为ini,名为pip的文件,另存为  (pip.ini) 文件内容设置为:(清华源) [glob ...

  9. 题解:CF593D Happy Tree Party

    题解:CF593D Happy Tree Party Description Bogdan has a birthday today and mom gave him a tree consistin ...

  10. JS获取指定月份的天数几种方法

    最近看到一个有意思的试题,正好在需求中也碰到类似的问题,即计算某个月的天数问题.碰到类似问题也许大部分会想是不是还要判断闰年.平年,如果这样想的话就复杂了,下面给出具体的计算方法. 获取月份天数方法一 ...