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 ...
随机推荐
- STM32的CCM RAM
STM32F407ZGT6的Flash大小为1MB,SRAM大小为(128KB+64KB). 这里SRAM之所以分开表示是因为在芯片内部前面的128KB和后面的64KB地址不是连续的,后面的64KB在 ...
- Java知识系统回顾整理01基础05控制流程08综合练习
一.练习--黄金分割点 题目: 寻找某两个数相除,其结果 离黄金分割点 0.618最近 分母和分子不能同时为偶数 分母和分子 取值范围在[1,20] (即1到20) 要求效果: public clas ...
- 00 what is C Programming?C编程是什么?
C语言简介 C is a programming language that lets us give a computer very specifio commands. C语言是一种编程语言,它让 ...
- SetDlgItemInt(函数详解)
参考:https://blog.csdn.net/for_cxc/article/details/51799194 SetDlgItemInt(hwnd, IDC_TEXT, FREQ_INIT, F ...
- dsu on tree 入门
Dus on tree 树上并查集?. 啊这,并不是的啦,他利用了树上启发式合并的思想. 他主要解决不带修改且主要询问子树信息的树上问题. 先来看到例题,CF600E . 这不就是树上莫队的经典题吗? ...
- # BlackLivesMatter !
下载 # BlackLivesMatter ! https://blacklivesmatter.com/ 黑人的生命是重要的运动资源 VueCroppie VueCroppie是一个Vue 2包装C ...
- 【MySQL Errors】Table 'xxx' is marked as crashed and should be repaired 的解决方案
现象描述 访问 Zabbix Web,出现如下错误提示: • Error in query [SELECT * FROM history_uint h WHERE h.itemid='25067' O ...
- 【LGR-070】洛谷 3 月月赛-官方题解
本次免费为大家提供[LGR-070]洛谷 3 月月赛的官方题解,点个赞再走呗! 代码就不上了,大家可以到别的博客上去找找!希望这篇博客能对你有所帮助!
- install Wine + WeChat in Fedora 31
install Wine + WeChat in Fedora 31 dnf -y install dnf-plugins-core dnf config-manager --add-repo htt ...
- MeteoInfoLab脚本示例:FY-2C 云分类HDF数据
脚本程序: #Add data file fn = 'D:/Temp/hdf/FY2C_CLC_MLT_NOM_20070730_1800.hdf' f = addfile(fn) #Get data ...