# Leetcode 391. Perfect Rectangle

## 解题思想

• 最左下/最左上/最右下/最右上的四个点只出现过一次,其他肯定是出现两次和四次(保证完全覆盖)
• 上面四个点围成的面积,正好等于所有子矩形的面积之和(保证不重复)

Leetcode代码收录，求粉求星星。

class Solution(object):
def isRectangleCover(self, rectangles):
"""
:type rectangles: List[List[int]]
:rtype: bool
"""
def recordCorner(point):
if point in corners:
corners[point] += 1
else:
corners[point] = 1

corners = {}
area =  0
left = min(x[0] for x in rectangles)
bottom = min(x[1] for x in rectangles)
right = max(x[2] for x in rectangles)
top = max(x[3] for x in rectangles)

for sub in rectangles:
ax, ay, bx, by = sub[:]
area += (bx-ax)*(by-ay)
map(recordCorner, [(ax, ay), (bx, by), (ax, by), (bx, ay)])

if area != (top-bottom)*(right-left): return False

big_four = [(left,bottom),(right,top),(left,top),(right,bottom)]

for bf in big_four:                         # check corners of big rectangle
if bf not in corners or corners[bf] != 1:
return False

for key in corners:                         # check existing "inner" points
if corners[key]%2 and key not in big_four:
return False

return True

• 本文已收录于以下专栏：

举报原因： 您举报文章：Leetcode 391. Perfect Rectangle 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)