解题思路:
关键在于对重复区域面积的计算
原题目:
Find the total area covered by two rectilinear rectangles in a 2D plane.
Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.
Assume that the total area is never beyond the maximum possible value of int.
AC解,C++代码,菜鸟一个,请大家多多指正
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if (C > G) {
return computeArea(E, F, G, H, A, B, C, D);
}
int area1 = (C - A) * (D - B);
int area2 = (G - E) * (H - F);
int area3 = 0;
if (C <= E || B >= H || D <= F) {
return area1 + area2;
}
area3 = (C - max(E, A)) * (min(H,D) - max(B, F));
return area1 + area2 - area3;
}
};