您的位置:首页 > 其它

【LeetCode】223 - Rectangle Area

2015-08-02 20:08 302 查看
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.

Solution: 两长方形面积之和减去重合部分面积;当A>=G或C<=E或B>=H或D<=F时,两长方形不相交;

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int width,height;
if(A>=G||C<=E||B>=H||D<=F){
width=0;
height=0;
}else{
if(A<=E)
width=min(C-E,G-E);
else
width=min(C-A,G-A);

if(B<=F)
height=min(D-F,H-F);
else
height=min(D-B,H-B);
}
return (C-A)*(D-B)+(G-E)*(H-F)-height*width;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: