Skip to content

Commit b13ac11

Browse files
committed
feat: add js solution to lc problem: No.0840
1 parent 0923a06 commit b13ac11

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

solution/0800-0899/0840.Magic Squares In Grid/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,52 @@ export function numMagicSquaresInside(grid: number[][]): number {
374374
}
375375
```
376376

377+
#### JavaScript
378+
379+
```js
380+
function numMagicSquaresInside(grid) {
381+
const [m, n] = [grid.length, grid[0].length];
382+
if (m < 3 || n < 3) return 0;
383+
384+
const check = (y, x) => {
385+
const g = grid;
386+
if (g[y + 1][x + 1] !== 5) return false;
387+
388+
const cells = [
389+
g[y][x],
390+
g[y][x + 1],
391+
g[y][x + 2],
392+
g[y + 1][x + 2],
393+
g[y + 2][x + 2],
394+
g[y + 2][x + 1],
395+
g[y + 2][x],
396+
g[y + 1][x],
397+
];
398+
399+
const i = cells.indexOf(2);
400+
if (i === -1) return false;
401+
cells.push(...cells.splice(0, i));
402+
403+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
404+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
405+
406+
if (cells.every((x, i) => x === circle[i])) return true;
407+
if (cells.every((x, i) => x === reverseCircle[i])) return true;
408+
409+
return false;
410+
};
411+
412+
let res = 0;
413+
for (let i = 0; i < m - 2; i++) {
414+
for (let j = 0; j < n - 2; j++) {
415+
res += +check(i, j);
416+
}
417+
}
418+
419+
return res;
420+
}
421+
```
422+
377423
<!-- tabs:end -->
378424

379425
<!-- solution:end -->

solution/0800-0899/0840.Magic Squares In Grid/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,52 @@ export function numMagicSquaresInside(grid: number[][]): number {
370370
}
371371
```
372372

373+
#### JavaScript
374+
375+
```js
376+
function numMagicSquaresInside(grid) {
377+
const [m, n] = [grid.length, grid[0].length];
378+
if (m < 3 || n < 3) return 0;
379+
380+
const check = (y, x) => {
381+
const g = grid;
382+
if (g[y + 1][x + 1] !== 5) return false;
383+
384+
const cells = [
385+
g[y][x],
386+
g[y][x + 1],
387+
g[y][x + 2],
388+
g[y + 1][x + 2],
389+
g[y + 2][x + 2],
390+
g[y + 2][x + 1],
391+
g[y + 2][x],
392+
g[y + 1][x],
393+
];
394+
395+
const i = cells.indexOf(2);
396+
if (i === -1) return false;
397+
cells.push(...cells.splice(0, i));
398+
399+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
400+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
401+
402+
if (cells.every((x, i) => x === circle[i])) return true;
403+
if (cells.every((x, i) => x === reverseCircle[i])) return true;
404+
405+
return false;
406+
};
407+
408+
let res = 0;
409+
for (let i = 0; i < m - 2; i++) {
410+
for (let j = 0; j < n - 2; j++) {
411+
res += +check(i, j);
412+
}
413+
}
414+
415+
return res;
416+
}
417+
```
418+
373419
<!-- tabs:end -->
374420

375421
<!-- solution:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function numMagicSquaresInside(grid) {
2+
const [m, n] = [grid.length, grid[0].length];
3+
if (m < 3 || n < 3) return 0;
4+
5+
const check = (y, x) => {
6+
const g = grid;
7+
if (g[y + 1][x + 1] !== 5) return false;
8+
9+
const cells = [
10+
g[y][x],
11+
g[y][x + 1],
12+
g[y][x + 2],
13+
g[y + 1][x + 2],
14+
g[y + 2][x + 2],
15+
g[y + 2][x + 1],
16+
g[y + 2][x],
17+
g[y + 1][x],
18+
];
19+
20+
const i = cells.indexOf(2);
21+
if (i === -1) return false;
22+
cells.push(...cells.splice(0, i));
23+
24+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
25+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
26+
27+
if (cells.every((x, i) => x === circle[i])) return true;
28+
if (cells.every((x, i) => x === reverseCircle[i])) return true;
29+
30+
return false;
31+
};
32+
33+
let res = 0;
34+
for (let i = 0; i < m - 2; i++) {
35+
for (let j = 0; j < n - 2; j++) {
36+
res += +check(i, j);
37+
}
38+
}
39+
40+
return res;
41+
}

0 commit comments

Comments
 (0)