您的位置:首页 > 其它

223. Rectangle Area (求两矩形重合部分的面积)

2016-10-28 09:41 330 查看
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.
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int sub = 0;
if(A>=G||E>=C||B>=H||F>=D)
sub = 0;
else{
int length1 = 0,length2=0;
if(H<D){
if(B>F)
length1=H-B;
else
length1=H-F;
}
else{
if(F>B)
length1=D-F;
else
length1=D-B;
}
if(A<E){
if(C<G)
length2=C-E;
else
length2=G-E;
}
else{
if(C>G)
length2=G-A;
else
length2=C-A;
}
sub = length1*length2;
}
return (D-B)*(C-A)+(G-E)*(H-F)-sub;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法