几何知识,综合考虑可能出现的条件就能解决了。
代码:
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int x1, y1;
int x2, y2;
x1 = E > A? E: A;
y1 = F > B? F: B;
x2 = C < G? C: G;
y2 = D < H? D: H;
int area1 = (C-A)*(D-B);
int area2 = (G-E)*(H-F);
int duplicateArea = (y2 > y1 && x2 > x1)? (y2-y1)*(x2-x1): 0;
return area1+area2-duplicateArea;
}
}