您的位置:首页 > 编程语言 > C语言/C++

Rectangle Area

2016-06-07 22:40 549 查看

c++

class Solution {
public:
int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int overlap = overlapArea(A, B, C, D, E, F, G, H);
return (C - A)*(D - B) + (G - E)*(H - F) - overlap;
}
private:
int overlapArea(int A, int B, int C, int D, int E, int F, int G, int H) {
int lhs_x = max(A, E);
int lhs_y = max(B, F);
int rhs_x = min(C, G);
int rhs_y = min(D, H);
if ((rhs_x < lhs_x) || (rhs_y < lhs_y))
return 0;
return (rhs_x - lhs_x) * (rhs_y - lhs_y);
}
};


python

class Solution(object):
def computeArea(self, A, B, C, D, E, F, G, H):
"""
:type A: int
:type B: int
:type C: int
:type D: int
:type E: int
:type F: int
:type G: int
:type H: int
:rtype: int
"""
lhs_x = max(A, E)
lhs_y = max(B, F)
rhs_x = min(C, G)
rhs_y = min(D, H)
if rhs_x<lhs_x or rhs_y<lhs_y:
overlap=0
else:
overlap=(rhs_x - lhs_x) * (rhs_y - lhs_y)
return (C - A)*(D - B) + (G - E)*(H - F) - overlap
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言