# [leetcode 223] Rectangle Area

215人阅读 评论(0)

Question:
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.

Rectangle Area
Assume that the total area is never beyond the maximum possible value of int.

<span style="font-size:14px;">class Solution {
private:
int area(int height, int width){
return height*width;
}
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
/*int result;
int area1,area2,area3;
area1 = area(D-B,C-A);
area2 = area(H-F,G-E);

if(E < A && F < B && G > C && H > D)
return area2;
if(A < E && B < F && C > G && D > H)
return area1;
if(A == E && B == F && C == G && D == H)
return area1;
if(C < E || G < A || F > D || H < B)
return area1+area2;
vector<int> Xsort{A,C,E,G};
vector<int> Ysort{B,D,F,H};
sort(Xsort.begin(),Xsort.end());
sort(Ysort.begin(),Ysort.end());
area3 = area(Ysort[2]-Ysort[1], Xsort[2]-Xsort[1]);
return area1 + area2 - area3;*/
int area1 = (C-A)*(D-B);
int area2 = (G-E)*(H-F);
if(C<=E || G<=A || D<=F ||B>=H){
return area1+area2;
}
int width =  (A<=E)?min(C-E,G-E):min(C-A,G-A);
int height = (B>=F)?min(D-B,H-B):min(D-F,H-F);
return area1+area2-width*height;

}
};</span>

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：41821次
• 积分：1621
• 等级：
• 排名：千里之外
• 原创：126篇
• 转载：6篇
• 译文：0篇
• 评论：1条
文章分类
评论排行
最新评论