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. 屯点自用CTF网站

    尚且杂乱,刚刚准备搬运东西到博客来,慢慢收拾. 芜湖,起飞  --大司 16进制转换文本 Base64编码转换 quipqiup词频分析 Brainfuck/Ook! Obfuscation/Enco ...

  2. 00 你的第一个C语言程序

    C语言简介 C 语言是一种通用的.面向过程式的计算机程序设计语言,即编程语言. 为移植和开发 UNIX 操作系统,丹尼斯·里奇于1972年在贝尔电话实验室设计开发了 C 语言的第一个版本. C 语言同 ...

  3. 【3】Java面试-Servlet

    Servlet面试问题 Q1.什么是servlet? Java Servlet是服务器端技术,通过提供对动态响应和数据持久性的支持来扩展Web服务器的功能. javax.servlet和javax.s ...

  4. C#实例(经典):四路光电开关&激光雷达数据采集和波形图绘制

    前言:本文全部纯手工打造,如有疏漏之处,还请谅解! 如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! 这 ...

  5. SpringBoot 完整学习笔记免费分享

    从0到进阶,完全系统性的学习笔记 每次我都会反复拿来观看,因为我们总会有遗漏忘记的地方,但是笔记不会. 希望大家能好好利用它,以下是笔记截图! 以上只是其中的一项部分,这份笔记可以说含金量超高,绝对会 ...

  6. <二分查找+双指针+前缀和>解决子数组和排序后的区间和

    <二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...

  7. 在Linux终端中快速生成、解码二维码

    我们要实现两个功能: 解码Linux屏幕上的二维码,将结果输出在终端 在终端中将字符串转为二维码,直接显示二维码在终端中以供扫描 实现方法 生成二维码 qrencode是一个常见的生成二维码的CLI程 ...

  8. linux(centos8):prometheus使用mtail监控错误日志

    一,mtail的用途? mtail :从应用程序日志中提取指标以导出到时间序列数据库或时间序列计算器 它是一个google开发的日志提取工具,用途就是: 实时读取应用程序的日志. 再通过自己编写的脚本 ...

  9. linux 压缩命令 zip

    1.zip命令 例如:zip -r mysql.zip mysql 该句命令的含义是:将mysql文件夹压缩成mysql.zip zip -r abcdef.zip abc def.txt 这句命令的 ...

  10. centos8平台使用strace跟踪系统调用

    一,strace的用途 strace  是最常用的跟踪进程系统调用的工具. 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectfore ...