【leetcode】223. Rectangle Area

178人阅读 评论(0)

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.

c++代码（32ms，56.67%）

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if(C<E || G<A || H<B || D<F)
return (C-A)*(D-B)+(G-E)*(H-F);
else{
//算出长和高
int length,height; //长,高
if(A<=E && E<=C && C<=G)
length=C-E;
else if(A<=E && E<=G && G<=C)
length=G-E;
else if(E<=A && A<=G && G<=C)
length=G-A;
else
length=C-A;

if(D>=H && H>=B && B>=F)
height=H-B;
else if(D>=H && H>=F && F>=B)
height=H-F;
else if(H>=D && D>=F && F>=B)
height=D-F;
else
height=D-B;

return (C-A)*(D-B)+(G-E)*(H-F)-length*height;

}

}
};

int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int left = max(A,E), right = max(min(C,G), left);
int bottom = max(B,F), top = max(min(D,H), bottom);
return (C-A)*(D-B) - (right-left)*(top-bottom) + (G-E)*(H-F);
}

0
0

个人资料
• 访问：223719次
• 积分：4285
• 等级：
• 排名：第8444名
• 原创：230篇
• 转载：0篇
• 译文：0篇
• 评论：76条
最新评论