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 6df3dbf

Browse files
authored
feat: add js/ts solutions to lc problem: No.0840 (#3393)
1 parent e5b5d95 commit 6df3dbf

File tree

5 files changed

+431
-0
lines changed

5 files changed

+431
-0
lines changed

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,158 @@ function numMagicSquaresInside(grid: number[][]): number {
318318
}
319319
```
320320

321+
#### JavaScript
322+
323+
```js
324+
function numMagicSquaresInside(grid) {
325+
const m = grid.length;
326+
const n = grid[0].length;
327+
const check = (i, j) => {
328+
if (i + 3 > m || j + 3 > n) {
329+
return 0;
330+
}
331+
const cnt = Array(16).fill(0);
332+
const row = Array(3).fill(0);
333+
const col = Array(3).fill(0);
334+
let [a, b] = [0, 0];
335+
for (let x = i; x < i + 3; ++x) {
336+
for (let y = j; y < j + 3; ++y) {
337+
const v = grid[x][y];
338+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
339+
return 0;
340+
}
341+
row[x - i] += v;
342+
col[y - j] += v;
343+
if (x - i === y - j) {
344+
a += v;
345+
}
346+
if (x - i === 2 - (y - j)) {
347+
b += v;
348+
}
349+
}
350+
}
351+
if (a !== b) {
352+
return 0;
353+
}
354+
for (let k = 0; k < 3; ++k) {
355+
if (row[k] !== a || col[k] !== a) {
356+
return 0;
357+
}
358+
}
359+
return 1;
360+
};
361+
let ans = 0;
362+
for (let i = 0; i < m; ++i) {
363+
for (let j = 0; j < n; ++j) {
364+
ans += check(i, j);
365+
}
366+
}
367+
return ans;
368+
}
369+
```
370+
371+
<!-- tabs:end -->
372+
373+
<!-- solution:end -->
374+
375+
<!-- solution:start -->
376+
377+
### Solution 2
378+
379+
<!-- tabs:start -->
380+
381+
#### TypeScript
382+
383+
```ts
384+
export function numMagicSquaresInside(grid: number[][]): number {
385+
const [m, n] = [grid.length, grid[0].length];
386+
if (m < 3 || n < 3) return 0;
387+
388+
const check = (y: number, x: number) => {
389+
const g = grid;
390+
if (g[y + 1][x + 1] !== 5) return 0;
391+
392+
const cells = [
393+
g[y][x],
394+
g[y][x + 1],
395+
g[y][x + 2],
396+
g[y + 1][x + 2],
397+
g[y + 2][x + 2],
398+
g[y + 2][x + 1],
399+
g[y + 2][x],
400+
g[y + 1][x],
401+
];
402+
403+
const i = cells.indexOf(2);
404+
if (i === -1) return 0;
405+
cells.push(...cells.splice(0, i));
406+
407+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
408+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
409+
410+
if (cells.every((x, i) => x === circle[i])) return 1;
411+
if (cells.every((x, i) => x === reverseCircle[i])) return 1;
412+
413+
return 0;
414+
};
415+
416+
let res = 0;
417+
for (let i = 0; i < m - 2; i++) {
418+
for (let j = 0; j < n - 2; j++) {
419+
res += check(i, j);
420+
}
421+
}
422+
423+
return res;
424+
}
425+
```
426+
427+
#### JavaScript
428+
429+
```js
430+
function numMagicSquaresInside(grid) {
431+
const [m, n] = [grid.length, grid[0].length];
432+
if (m < 3 || n < 3) return 0;
433+
434+
const check = (y, x) => {
435+
const g = grid;
436+
if (g[y + 1][x + 1] !== 5) return false;
437+
438+
const cells = [
439+
g[y][x],
440+
g[y][x + 1],
441+
g[y][x + 2],
442+
g[y + 1][x + 2],
443+
g[y + 2][x + 2],
444+
g[y + 2][x + 1],
445+
g[y + 2][x],
446+
g[y + 1][x],
447+
];
448+
449+
const i = cells.indexOf(2);
450+
if (i === -1) return false;
451+
cells.push(...cells.splice(0, i));
452+
453+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
454+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
455+
456+
if (cells.every((x, i) => x === circle[i])) return true;
457+
if (cells.every((x, i) => x === reverseCircle[i])) return true;
458+
459+
return false;
460+
};
461+
462+
let res = 0;
463+
for (let i = 0; i < m - 2; i++) {
464+
for (let j = 0; j < n - 2; j++) {
465+
res += +check(i, j);
466+
}
467+
}
468+
469+
return res;
470+
}
471+
```
472+
321473
<!-- tabs:end -->
322474

323475
<!-- solution:end -->

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,158 @@ function numMagicSquaresInside(grid: number[][]): number {
314314
}
315315
```
316316

317+
#### JavaScript
318+
319+
```js
320+
function numMagicSquaresInside(grid) {
321+
const m = grid.length;
322+
const n = grid[0].length;
323+
const check = (i, j) => {
324+
if (i + 3 > m || j + 3 > n) {
325+
return 0;
326+
}
327+
const cnt = Array(16).fill(0);
328+
const row = Array(3).fill(0);
329+
const col = Array(3).fill(0);
330+
let [a, b] = [0, 0];
331+
for (let x = i; x < i + 3; ++x) {
332+
for (let y = j; y < j + 3; ++y) {
333+
const v = grid[x][y];
334+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
335+
return 0;
336+
}
337+
row[x - i] += v;
338+
col[y - j] += v;
339+
if (x - i === y - j) {
340+
a += v;
341+
}
342+
if (x - i === 2 - (y - j)) {
343+
b += v;
344+
}
345+
}
346+
}
347+
if (a !== b) {
348+
return 0;
349+
}
350+
for (let k = 0; k < 3; ++k) {
351+
if (row[k] !== a || col[k] !== a) {
352+
return 0;
353+
}
354+
}
355+
return 1;
356+
};
357+
let ans = 0;
358+
for (let i = 0; i < m; ++i) {
359+
for (let j = 0; j < n; ++j) {
360+
ans += check(i, j);
361+
}
362+
}
363+
return ans;
364+
}
365+
```
366+
367+
<!-- tabs:end -->
368+
369+
<!-- solution:end -->
370+
371+
<!-- solution:start -->
372+
373+
### Solution 2
374+
375+
<!-- tabs:start -->
376+
377+
#### TypeScript
378+
379+
```ts
380+
export function numMagicSquaresInside(grid: number[][]): number {
381+
const [m, n] = [grid.length, grid[0].length];
382+
if (m < 3 || n < 3) return 0;
383+
384+
const check = (y: number, x: number) => {
385+
const g = grid;
386+
if (g[y + 1][x + 1] !== 5) return 0;
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 0;
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 1;
407+
if (cells.every((x, i) => x === reverseCircle[i])) return 1;
408+
409+
return 0;
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+
423+
#### JavaScript
424+
425+
```js
426+
function numMagicSquaresInside(grid) {
427+
const [m, n] = [grid.length, grid[0].length];
428+
if (m < 3 || n < 3) return 0;
429+
430+
const check = (y, x) => {
431+
const g = grid;
432+
if (g[y + 1][x + 1] !== 5) return false;
433+
434+
const cells = [
435+
g[y][x],
436+
g[y][x + 1],
437+
g[y][x + 2],
438+
g[y + 1][x + 2],
439+
g[y + 2][x + 2],
440+
g[y + 2][x + 1],
441+
g[y + 2][x],
442+
g[y + 1][x],
443+
];
444+
445+
const i = cells.indexOf(2);
446+
if (i === -1) return false;
447+
cells.push(...cells.splice(0, i));
448+
449+
const circle = [2, 9, 4, 3, 8, 1, 6, 7];
450+
const reverseCircle = [2, 7, 6, 1, 8, 3, 4, 9];
451+
452+
if (cells.every((x, i) => x === circle[i])) return true;
453+
if (cells.every((x, i) => x === reverseCircle[i])) return true;
454+
455+
return false;
456+
};
457+
458+
let res = 0;
459+
for (let i = 0; i < m - 2; i++) {
460+
for (let j = 0; j < n - 2; j++) {
461+
res += +check(i, j);
462+
}
463+
}
464+
465+
return res;
466+
}
467+
```
468+
317469
<!-- tabs:end -->
318470

319471
<!-- solution:end -->
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function numMagicSquaresInside(grid) {
2+
const m = grid.length;
3+
const n = grid[0].length;
4+
const check = (i, j) => {
5+
if (i + 3 > m || j + 3 > n) {
6+
return 0;
7+
}
8+
const cnt = Array(16).fill(0);
9+
const row = Array(3).fill(0);
10+
const col = Array(3).fill(0);
11+
let [a, b] = [0, 0];
12+
for (let x = i; x < i + 3; ++x) {
13+
for (let y = j; y < j + 3; ++y) {
14+
const v = grid[x][y];
15+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
16+
return 0;
17+
}
18+
row[x - i] += v;
19+
col[y - j] += v;
20+
if (x - i === y - j) {
21+
a += v;
22+
}
23+
if (x - i === 2 - (y - j)) {
24+
b += v;
25+
}
26+
}
27+
}
28+
if (a !== b) {
29+
return 0;
30+
}
31+
for (let k = 0; k < 3; ++k) {
32+
if (row[k] !== a || col[k] !== a) {
33+
return 0;
34+
}
35+
}
36+
return 1;
37+
};
38+
let ans = 0;
39+
for (let i = 0; i < m; ++i) {
40+
for (let j = 0; j < n; ++j) {
41+
ans += check(i, j);
42+
}
43+
}
44+
return ans;
45+
}

0 commit comments

Comments
(0)

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