您的位置:首页 > 其它

【LEETCODE】223-Rectangle Area

2015-12-28 12:23 274 查看
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.

题意:
找到两个 二维平面上的直线 矩形的并集的面积
每个矩形由左下角和右上角的坐标决定
假设整体面积不会超过int的最大值

思路:
求出两个矩形的面积,再减去交集的面积,注意不相交时,直接两个面积相加



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
"""

la=C-A
wa=D-B
lb=G-E
wb=H-F

sa=la*wa
sb=lb*wb

if C<=E or G<=A or D<=F or H<=B:        #这些就是不相交的情况
return sa+sb
else:

lset=sorted([A,C,E,G])              #直接用sort,没有考虑到不相交的情况
lab=lset[2]-lset[1]
wset=sorted([B,D,F,H])
wab=wset[2]-wset[1]

sab=lab*wab

return sa+sb-sab
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: