本文主要是介绍Python | Leetcode Python题解之第391题完美矩形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution:def isRectangleCover(self, rectangles: List[List[int]]) -> bool:area, minX, minY, maxX, maxY = 0, rectangles[0][0], rectangles[0][1], rectangles[0][2], rectangles[0][3]cnt = defaultdict(int)for rect in rectangles:x, y, a, b = rect[0], rect[1], rect[2], rect[3]area += (a - x) * (b - y)minX = min(minX, x)minY = min(minY, y)maxX = max(maxX, a)maxY = max(maxY, b)cnt[(x, y)] += 1cnt[(x, b)] += 1cnt[(a, y)] += 1cnt[(a, b)] += 1if area != (maxX - minX) * (maxY - minY) or cnt[(minX, minY)] != 1 or cnt[(minX, maxY)] != 1 or cnt[(maxX, minY)] != 1 or cnt[(maxX, maxY)] != 1:return Falsedel cnt[(minX, minY)], cnt[(minX, maxY)], cnt[(maxX, minY)], cnt[(maxX, maxY)]return all(c == 2 or c == 4 for c in cnt.values())
这篇关于Python | Leetcode Python题解之第391题完美矩形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!