223. 矩形面积
题目链接
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/rectangle-area/submissions/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题目描述
题目分析
要注意一下两个矩形没有重叠的情况。
class Solution {
public:
int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int s1 = (ax2 - ax1) * (ay2 - ay1);
int s2 = (bx2 - bx1) * (by2 - by1);
// 两个矩形没有重叠
if(by1 > ay2 || bx1 > ax2 || bx2 < ax1 || by2 < ay1){
return s1 + s2;
}
int lap = (min(ax2, bx2) - max(ax1, bx1)) * (min(ay2, by2) - max(ay1, by1));
return s1 + s2 - lap;
}
};