乱七八糟
跟我的人生依样
一二三四五
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if i+2<m and j+2<n:
cnt = set()
for ii in range(i,i+3):
for jj in range(j,j+3):
if grid[ii][jj] <= 9:
cnt.add(grid[ii][jj])
if len(cnt) != 9:
continue
sums = []
# row
for k in range(3):
sums.append(grid[i+k][j]+grid[i+k][j+1]+grid[i+k][j+2])
# col
for k in range(3):
sums.append(grid[i][j+k]+grid[i+1][j+k]+grid[i+2][j+k])
# diagonal
sums.append(grid[i][j]+grid[i+1][j+1]+grid[i+2][j+2])
sums.append(grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j])
if all([s==15 for s in sums]):
ans += 1
return ans