本题不清楚题意,从网上找到了python的解答,记录如下。
class Solution: def numMagicSquaresInside(self, grid): ans, lrc = 0, [len(grid), len(grid[0])] def checkMagic(a,b, c, d, e, f, g ,h, i): return (sorted([a,b,c,d,e,f,g,h,i]) == [i for i in range(1,10)] and (a + b+c == d + e + f == g + h + i == a + d + g == b + e + h == c +f + i == a + e + i == c + e + g == 15)) for i in range(1, lrc[0]-1): for j in range(1, lrc[1]-1): if grid[i][j] == 5 and checkMagic(grid[i-1][j-1], grid[i-1][j], grid[i-1][j+1], grid[i][j-1], grid[i][j], grid[i][j+1], grid[i+1][j-1], grid[i+1][j], grid[i+1][j+1]): ans += 1 return ans