Description
中文
English
给定两个矩形,判断这两个矩形是否有重叠。
l1代表第一个矩形的左上角
r1代表第一个矩形的右下角
l2代表第二个矩形的左上角
r2代表第二个矩形的右下角
保证:l1 != r1 并且 l2 != r2
Have you met this question in a real interview?
Example
样例 1:
输入 : l1 = [0, 8], r1 = [8, 0], l2 = [6, 6], r2 = [10, 0]
输出 : true
样例 2:
输入 : [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
//派出不相交的情况
return !((l2.x>r1.x) ||(r1.y>l2.y) ||(r2.x<l1.x) ||(l1.y<r2.y));
}
};