Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c8e22db

Browse files
840. Magic Squares In Grid
1 parent 50e6123 commit c8e22db

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎Hard/840. Magic Spquares In Grid‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const numMagicSquaresInside = grid => {
2+
const magicSquares = new Set([
3+
'276951438',
4+
'294753618',
5+
'438951276',
6+
'492357816',
7+
'618753294',
8+
'672159834',
9+
'816357492',
10+
'834159672',
11+
]);
12+
let cnt = 0;
13+
for (let i = 0; i < grid.length - 2; i++)
14+
for (let j = 0; j < grid[0].length - 2; j++)
15+
if (
16+
magicSquares.has(
17+
grid[i][j].toString() +
18+
grid[i][j + 1] +
19+
grid[i][j + 2] +
20+
grid[i + 1][j] +
21+
grid[i + 1][j + 1] +
22+
grid[i + 1][j + 2] +
23+
grid[i + 2][j] +
24+
grid[i + 2][j + 1] +
25+
grid[i + 2][j + 2],
26+
)
27+
)
28+
cnt++;
29+
return cnt;
30+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /