Skip to content

Commit e9f8baf

Browse files
authored
feat: add Rust solution for lc No.0840 (#4942)
1 parent 0aed8a5 commit e9f8baf

File tree

5 files changed

+201
-288
lines changed

5 files changed

+201
-288
lines changed

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

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

321+
#### Rust
322+
323+
```rust
324+
impl Solution {
325+
pub fn num_magic_squares_inside(grid: Vec<Vec<i32>>) -> i32 {
326+
let m = grid.len();
327+
let n = grid[0].len();
328+
let mut ans: i32 = 0;
329+
330+
let check = |i: usize, j: usize, grid: &Vec<Vec<i32>>| -> i32 {
331+
if i + 3 > m || j + 3 > n {
332+
return 0;
333+
}
334+
335+
let mut cnt = vec![0; 16];
336+
let mut row = vec![0; 3];
337+
let mut col = vec![0; 3];
338+
let mut a = 0;
339+
let mut b = 0;
340+
341+
for x in i..i + 3 {
342+
for y in j..j + 3 {
343+
let v = grid[x][y] as usize;
344+
if v < 1 || v > 9 {
345+
return 0;
346+
}
347+
cnt[v] += 1;
348+
if cnt[v] > 1 {
349+
return 0;
350+
}
351+
352+
let vv = grid[x][y];
353+
row[x - i] += vv;
354+
col[y - j] += vv;
355+
356+
if x - i == y - j {
357+
a += vv;
358+
}
359+
if x - i + y - j == 2 {
360+
b += vv;
361+
}
362+
}
363+
}
364+
365+
if a != b {
366+
return 0;
367+
}
368+
369+
for k in 0..3 {
370+
if row[k] != a || col[k] != a {
371+
return 0;
372+
}
373+
}
374+
375+
1
376+
};
377+
378+
for i in 0..m {
379+
for j in 0..n {
380+
ans += check(i, j, &grid);
381+
}
382+
}
383+
384+
ans
385+
}
386+
}
387+
```
388+
321389
#### JavaScript
322390

323391
```js
@@ -372,106 +440,4 @@ function numMagicSquaresInside(grid) {
372440

373441
<!-- solution:end -->
374442

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-
473-
<!-- tabs:end -->
474-
475-
<!-- solution:end -->
476-
477443
<!-- problem:end -->

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

Lines changed: 70 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ In total, there is only one magic square inside the given grid.
6464

6565
### Solution 1: Enumeration
6666

67-
We directly enumerate the top-left coordinates $(i, j)$ of each $3 \times 3$ sub-matrix, then check whether the sub-matrix satisfies the "magic square" condition. If it does, increment the answer by one. After enumeration, return the answer.
67+
We directly enumerate the top-left coordinates $(i, j)$ of each $3 \times 3$ submatrix, then check whether the submatrix satisfies the "magic square" property. If so, we increment the answer by one. After the enumeration, we return the answer.
6868

69-
Time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. Space complexity is $O(1)$.
69+
The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the matrix, respectively. The space complexity is $O(1)$.
7070

7171
<!-- tabs:start -->
7272

@@ -314,6 +314,74 @@ function numMagicSquaresInside(grid: number[][]): number {
314314
}
315315
```
316316

317+
#### Rust
318+
319+
```rust
320+
impl Solution {
321+
pub fn num_magic_squares_inside(grid: Vec<Vec<i32>>) -> i32 {
322+
let m = grid.len();
323+
let n = grid[0].len();
324+
let mut ans: i32 = 0;
325+
326+
let check = |i: usize, j: usize, grid: &Vec<Vec<i32>>| -> i32 {
327+
if i + 3 > m || j + 3 > n {
328+
return 0;
329+
}
330+
331+
let mut cnt = vec![0; 16];
332+
let mut row = vec![0; 3];
333+
let mut col = vec![0; 3];
334+
let mut a = 0;
335+
let mut b = 0;
336+
337+
for x in i..i + 3 {
338+
for y in j..j + 3 {
339+
let v = grid[x][y] as usize;
340+
if v < 1 || v > 9 {
341+
return 0;
342+
}
343+
cnt[v] += 1;
344+
if cnt[v] > 1 {
345+
return 0;
346+
}
347+
348+
let vv = grid[x][y];
349+
row[x - i] += vv;
350+
col[y - j] += vv;
351+
352+
if x - i == y - j {
353+
a += vv;
354+
}
355+
if x - i + y - j == 2 {
356+
b += vv;
357+
}
358+
}
359+
}
360+
361+
if a != b {
362+
return 0;
363+
}
364+
365+
for k in 0..3 {
366+
if row[k] != a || col[k] != a {
367+
return 0;
368+
}
369+
}
370+
371+
1
372+
};
373+
374+
for i in 0..m {
375+
for j in 0..n {
376+
ans += check(i, j, &grid);
377+
}
378+
}
379+
380+
ans
381+
}
382+
}
383+
```
384+
317385
#### JavaScript
318386

319387
```js
@@ -368,106 +436,4 @@ function numMagicSquaresInside(grid) {
368436

369437
<!-- solution:end -->
370438

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-
469-
<!-- tabs:end -->
470-
471-
<!-- solution:end -->
472-
473439
<!-- problem:end -->

0 commit comments

Comments
 (0)