【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.

【LeetCode】223 - Rectangle Area

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=;
height=;
}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;
}
};
上一篇:【LeetCode】836. Rectangle Overlap 解题报告(Python)


下一篇:js-new、object.create、bind的模拟实现【转载备忘】