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.
思路:找到(A,C)与(E,G)、(B,D)与(F,H)的交集,如果交集存在则说明有重合
如果有重合则计算两个矩形的面积后减去重合部分面积
没有重合则计算两个矩形面积相加即可
AC后为打败40%+的提交
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int x1=Math.max(A, E);
int x2=Math.min(C, G);
int y1=Math.max(B, F);
int y2=Math.min(H, D);
if(x2<x1||y1>y2){
return (C-A)*(D-B)+(G-E)*(H-F);
}else{
return (C-A)*(D-B)+(G-E)*(H-F)-(x2-x1)*(y2-y1);
}
}
}