每日一题.836. 矩形重叠

矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。

如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。

给出两个矩形,判断它们是否重叠并返回结果。

解题思路:纸上画图解决

class Solution {
public:
    bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
        if(rec2[0]<rec1[2] &&rec2[2]>rec1[0]){//rec2[0]<rec1[2]&&rec2[2]>rec1[0]说明rec2的x方向上与rec1有交集
            if(rec2[1]<rec1[3] && rec2[3]>rec1[1]){//rec2[1]<rec1[3] && rec2[3]>rec1[1]说明rec2的y方向上与rec1有交集
                return true;
            }
        }
        return false;
    }   
};

在这里插入图片描述

发布了35 篇原创文章 · 获赞 27 · 访问量 489

猜你喜欢

转载自blog.csdn.net/weixin_45221477/article/details/104942650