地址:点击打开链接
题目要求技术两个矩形覆盖面积,如果相交则减去两个矩形面积相交的部分,如果不相交则仅算两个矩形的面积
答案:
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
"""
part1 = (D-B)*(C-A)
part2 = (G-E)*(H-F)
#判断是否相交
if E > C or G < A or D < F or H < B:
return part1 + part2
partIntersection = (min(C,G) - max(E,A))*(min(D,H) - max(B,F))
return part1 + part2 - partIntersection