File tree 1 file changed +50
-0
lines changed
1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numMagicSquaresInside (self , grid : List [List [int ]]) -> int :
3
+ def isMagicSquare (grid ):
4
+ s = sum (grid [0 ])
5
+ hash = set ()
6
+ for i in grid :
7
+ for j in i :
8
+ if not 0 < j <= 9 :
9
+ return False
10
+ hash .add (j )
11
+ if len (hash )!= 9 :
12
+ return False
13
+ # row
14
+ for i in grid :
15
+ if sum (i )!= s :
16
+ return False
17
+ # col
18
+ for i in range (3 ):
19
+ colSum = 0
20
+ for j in range (3 ):
21
+ colSum += grid [j ][i ]
22
+ if colSum != s :
23
+ return False
24
+ # diagonal
25
+ if grid [0 ][0 ]+ grid [1 ][1 ]+ grid [2 ][2 ]!= s or grid [0 ][2 ]+ grid [1 ][1 ]+ grid [2 ][0 ]!= s :
26
+ return False
27
+ return True
28
+
29
+ if len (grid )< 3 or len (grid [0 ])< 3 :
30
+ return 0
31
+ cnt = 0
32
+ t ,b = 0 ,3
33
+ while t <= len (grid )- 3 :
34
+ l ,r = 0 ,3
35
+ while l <= len (grid [0 ])- 3 :
36
+ temp = [[0 for i in range (3 )] for i in range (3 )]
37
+ x = 0
38
+ for i in range (t ,b ):
39
+ y = 0
40
+ for j in range (l ,r ):
41
+ temp [x ][y ] = grid [i ][j ]
42
+ y += 1
43
+ x += 1
44
+ if isMagicSquare (temp ):
45
+ cnt += 1
46
+ l += 1
47
+ r += 1
48
+ t += 1
49
+ b += 1
50
+ return cnt
You can’t perform that action at this time.
0 commit comments