描述
给定两个矩形,判断这两个矩形是否有重叠。
l1代表第一个矩形的左上角
r1代表第一个矩形的右下角
l2代表第二个矩形的左上角
r2代表第二个矩形的右下角
保证:l1 != r2 并且 l2 != r2
您在真实的面试中是否遇到过这个题?
是
样例
给定 l1 = [0, 8]
, r1 = [8, 0]
, l2 = [6, 6]
, r2 = [10, 0]
, 返回 true
给定 l1 = [0, 8]
, r1 = [8, 0]
, l2 = [9, 6]
, r2 = [10, 0]
, 返回 false
只是一道小学奥数题,逆向思维,不重叠更好找一些。
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
/**
* @param l1: top-left coordinate of first rectangle
* @param r1: bottom-right coordinate of first rectangle
* @param l2: top-left coordinate of second rectangle
* @param r2: bottom-right coordinate of second rectangle
* @return: true if they are overlap or false
*/
bool doOverlap(Point &l1, Point &r1, Point &l2, Point &r2) {
// write your code here
if(l2.x>r1.x||r2.x<l1.x||r2.y>l1.y||l2.y<r1.y) return false;
else return true;
}
};