本题不清楚题意,从网上找到了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

 

posted on 2018-10-01 16:18  Sempron2800+  阅读(98)  评论(0编辑  收藏  举报