# LeetCode : 223. Rectangle Area

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.

1.判断是否相交

2.求出相交矩形的面积。

AC代码如下：

 int computeArea(int A, int B, int C, int D, int E, int F, int G, int H)
{
if(!IsInsection( A, B, C, D, E, F, G, H))
return (C-A)*(D-B)+(G-E)*(H-F);
else
return (C-A)*(D-B)+(G-E)*(H-F) - InsectionArea( A, B, C, D, E, F, G, H);
}
bool IsInsection(int A, int B, int C, int D, int E, int F, int G, int H)//判断两个矩形是否相交
{
if(D < F || C < E || H < B || G < A)
return false;
else
return true;
}
int InsectionArea(int A, int B, int C, int D, int E, int F, int G, int H)//相交矩形的面积
{
int minx = max(A,E);
int miny = max(B,F);
int maxx = min(C,G);
int maxy = min(D,H);
return (maxx - minx)*(maxy - miny);
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：LeetCode : 223. Rectangle Area 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)