Skip to content

Commit 0923a06

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

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,60 @@ function numMagicSquaresInside(grid: number[][]): number {
322322

323323
<!-- solution:end -->
324324

325+
<!-- solution:start -->
326+
327+
### Solution 2
328+
329+
<!-- tabs:start -->
330+
331+
#### TypeScript
332+
333+
```ts
334+
export function numMagicSquaresInside(grid: number[][]): number {
335+
const [m, n] = [grid.length, grid[0].length]
336+
if (m < 3 || n < 3) return 0
337+
338+
const check = (y: number, x: number) => {
339+
const g = grid
340+
if (g[y + 1][x + 1] !== 5) return 0
341+
342+
const cells = [
343+
g[y][x],
344+
g[y][x + 1],
345+
g[y][x + 2],
346+
g[y + 1][x + 2],
347+
g[y + 2][x + 2],
348+
g[y + 2][x + 1],
349+
g[y + 2][x],
350+
g[y + 1][x],
351+
]
352+
353+
const i = cells.indexOf(2)
354+
if (i === -1) return 0
355+
cells.push(...cells.splice(0, i))
356+
357+
const circle = [2, 9, 4, 3, 8, 1, 6, 7]
358+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]
359+
360+
if (cells.every((x, i) => x === circle[i])) return 1
361+
if (cells.every((x, i) => x === reverseCircle[i])) return 1
362+
363+
return 0
364+
}
365+
366+
let res = 0
367+
for (let i = 0; i < m - 2; i++) {
368+
for (let j = 0; j < n - 2; j++) {
369+
res += check(i, j)
370+
}
371+
}
372+
373+
return res
374+
}
375+
```
376+
377+
<!-- tabs:end -->
378+
379+
<!-- solution:end -->
380+
325381
<!-- problem:end -->

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,60 @@ function numMagicSquaresInside(grid: number[][]): number {
318318

319319
<!-- solution:end -->
320320

321+
<!-- solution:start -->
322+
323+
### Solution 2
324+
325+
<!-- tabs:start -->
326+
327+
#### TypeScript
328+
329+
```ts
330+
export function numMagicSquaresInside(grid: number[][]): number {
331+
const [m, n] = [grid.length, grid[0].length]
332+
if (m < 3 || n < 3) return 0
333+
334+
const check = (y: number, x: number) => {
335+
const g = grid
336+
if (g[y + 1][x + 1] !== 5) return 0
337+
338+
const cells = [
339+
g[y][x],
340+
g[y][x + 1],
341+
g[y][x + 2],
342+
g[y + 1][x + 2],
343+
g[y + 2][x + 2],
344+
g[y + 2][x + 1],
345+
g[y + 2][x],
346+
g[y + 1][x],
347+
]
348+
349+
const i = cells.indexOf(2)
350+
if (i === -1) return 0
351+
cells.push(...cells.splice(0, i))
352+
353+
const circle = [2, 9, 4, 3, 8, 1, 6, 7]
354+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9]
355+
356+
if (cells.every((x, i) => x === circle[i])) return 1
357+
if (cells.every((x, i) => x === reverseCircle[i])) return 1
358+
359+
return 0
360+
}
361+
362+
let res = 0
363+
for (let i = 0; i < m - 2; i++) {
364+
for (let j = 0; j < n - 2; j++) {
365+
res += check(i, j)
366+
}
367+
}
368+
369+
return res
370+
}
371+
```
372+
373+
<!-- tabs:end -->
374+
375+
<!-- solution:end -->
376+
321377
<!-- problem:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
function numMagicSquaresInside(grid: number[][]): number {
2+
const [m, n] = [grid.length, grid[0].length];
3+
if (m < 3 || n < 3) return 0;
4+
5+
const check = (y: number, x: number) => {
6+
const g = grid;
7+
if (g[y + 1][x + 1] !== 5) return 0;
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 0;
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 1;
28+
if (cells.every((x, i) => x === reverseCircle[i])) return 1;
29+
30+
return 0;
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)