836. Rectangle Overlap ——weekly contest 85
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:
Both rectangles
rec1andrec2are lists of 4 integers.- All coordinates in rectangles will be between
-10^9and10^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的更多相关文章
- 【Leetcode_easy】836. Rectangle Overlap
problem 836. Rectangle Overlap solution: class Solution { public: bool isRectangleOverlap(vector< ...
- #Leetcode# 836. Rectangle Overlap
https://leetcode.com/problems/rectangle-overlap/ A rectangle is represented as a list [x1, y1, x2, y ...
- [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 ...
- 836. Rectangle Overlap 矩形重叠
[抄题]: A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of i ...
- 【LeetCode】836. Rectangle Overlap 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/rectangle ...
- 835. Image Overlap —— weekly contest 84
Image Overlap Two images A and B are given, represented as binary, square matrices of the same size. ...
- 836. Rectangle Overlap
class Solution { public: bool isRectangleOverlap(vector<int>& rec1, vector<int>& ...
- 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 ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
随机推荐
- django rest_framework serializer的ManyRelatedField 和 SlugRelatedField使用
class BlogListSerializer(serializers.Serializer): id = serializers.IntegerField() user = BlogUserInf ...
- Java知识系统回顾整理01基础01第一个程序03Eclipse下载安装
Eclipse是最流行的java 集成开发环境IDE(Integrated Development Environment) 下载安装Eclipse两种方式 一.方式1:Eclipse官网下载安装 链 ...
- error C2491: 不允许 dllimport 函数 的定义
转载:https://blog.csdn.net/gaofeidongdong/article/details/7781345 在工程属性中 预编译宏中加上 DLL_EXPORT为了减少使用dll时候 ...
- Matlab中加汉明窗 ahmming 作用
转自:http://www.cnblogs.com/lxy2017/p/4049124.html 1.什么是汉明窗? 语音信号一般在10ms到30ms之间,我们可以把它看成是平稳的.为了处理语音信号, ...
- 使用类模板的C++线性表实现(数组方式)
main.h #ifndef _MAIN_H_ #define _MAIN_H_ #include <iostream> #include <exception> #inclu ...
- Jmeter之『如果(If)控制器』
判断方法 ${__jexl3("${projectName}"=="${targetDir}",)} ${__groovy("${projectNam ...
- NOIP提高组2013 D2T3 【华容道】
某王 老师给我们考了一场noip2013的真题...心态爆炸! 题目大意: 有一个n*m的棋盘,每个格子上都有一个棋子,有些格子上的棋子能够移动(可移动的棋子是固定的),棋盘中有一个格子是空的,仍何 ...
- C# 主界面的扁平化
如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! --------------------------- ...
- BUUCTF_web_三
下面还是简单web的入门题 [GKCTF2020]cve版签到 这次的比赛最简单的题了吧,提示是CVE-2020-7066,但是网上没有几个关于这个漏洞的相关利用的文章,似乎是get_header() ...
- win10简单方法安装杜比v4音效!win10 1909适用!
先下载这个! 链接: https://pan.baidu.com/s/1zAOOf-1aCJsjBgy36SiGWA 密码: s9n7 这个是杜比V4文件,257MB大小,适用32位64位系统!下 载 ...