题目:
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.
思路:
总的减去重复的即可
class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if(A==C||B==D) return (G-E)*(H-F);
if(G==E||H==F) return (C-A)*(D-B);
int sum=(G-E)*(H-F)+(C-A)*(D-B);
if(C<E||A>G||D<F||B>H) {
return sum;
}
return sum-abs((min(C,G)-max(E,A))*(min(D,H)-max(B,F)));
}
};