[leetcode 2022-01-27] 三、检测正方形

在这里插入图片描述

实现类
在这里插入图片描述

在上面的例子中,3-11-10-2

class DetectSquares:

    def __init__(self):
        self.map = defaultdict(Counter) # 新成立字典

    def add(self, point: List[int]) -> None:
        x, y = point  # 获取 x y
        self.map[y][x] += 1  #字典计数  # 键为y

    def count(self, point: List[int]) -> int:
        res = 0
        x, y = point

        if not y in self.map:
            return 0
        yCnt = self.map[y]  # x

        for col, colCnt in self.map.items():  # 值  键
            if col != y:
                # 根据对称性,这里可以不用取绝对值
                d = col - y
                res += colCnt[x] * yCnt[x + d] * colCnt[x + d]
                res += colCnt[x] * yCnt[x - d] * colCnt[x - d]
        
        return res

猜你喜欢

转载自blog.csdn.net/weixin_45492560/article/details/122710886