-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_Magic_Grid.java
68 lines (59 loc) · 2.09 KB
/
LC_Magic_Grid.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Solution {
public int numMagicSquaresInside(int[][] grid) {
int ans=0;
int m=grid.length;
int n= grid[0].length;
for(int row=0; row+2<m ; row++){
for(int col=0; col+2<n ; col++){
if(isMagicSquare(grid,row,col)){
ans++;
}
}
}
return ans;
}
private boolean isMagicSquare( int[][] grid, int row, int col){
boolean[] seen= new boolean[10];
for (int i=0; i<3; i++){
for(int j=0; j<3; j++){
int num= grid[row +i][col+j];
if(num<1 || num>9) return false;
if(seen[num]) return false;
seen[num]= true;
}
}
// check if diagonal sums are same value
int diagonal1 =
grid[row][col] + grid[row + 1][col + 1] + grid[row + 2][col + 2];
int diagonal2 =
grid[row + 2][col] + grid[row + 1][col + 1] + grid[row][col + 2];
if (diagonal1 != diagonal2) return false;
// check if all row sums share the same value as the diagonal sums
int row1 = grid[row][col] + grid[row][col + 1] + grid[row][col + 2];
int row2 =
grid[row + 1][col] +
grid[row + 1][col + 1] +
grid[row + 1][col + 2];
int row3 =
grid[row + 2][col] +
grid[row + 2][col + 1] +
grid[row + 2][col + 2];
if (!(row1 == diagonal1 && row2 == diagonal1 && row3 == diagonal1)) {
return false;
}
// check if all column sums share same value as the diagonal sums
int col1 = grid[row][col] + grid[row + 1][col] + grid[row + 2][col];
int col2 =
grid[row][col + 1] +
grid[row + 1][col + 1] +
grid[row + 2][col + 1];
int col3 =
grid[row][col + 2] +
grid[row + 1][col + 2] +
grid[row + 2][col + 2];
if (!(col1 == diagonal1 && col2 == diagonal1 && col3 == diagonal2)) {
return false;
}
return true;
}
}