Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0840 #1371

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 238 additions & 0 deletions solution/0800-0899/0840.Magic Squares In Grid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,260 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:枚举**

我们直接枚举每个 $3 \times 3$ 子矩阵的左上角坐标 $(i, j)$,然后判断该子矩阵是否满足“幻方矩阵”,若是,答案加一。枚举结束后,返回答案即可。

时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
def check(i: int, j: int) -> int:
if i + 3 > m or j + 3 > n:
return 0
s = set()
row = [0] * 3
col = [0] * 3
a = b = 0
for x in range(i, i + 3):
for y in range(j, j + 3):
v = grid[x][y]
if v < 1 or v > 9:
return 0
s.add(v)
row[x - i] += v
col[y - j] += v
if x - i == y - j:
a += v
if x - i == 2 - (y - j):
b += v
if len(s) != 9 or a != b:
return 0
if any(x != a for x in row) or any(x != a for x in col):
return 0
return 1

m, n = len(grid), len(grid[0])
return sum(check(i, j) for i in range(m) for j in range(n))
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
private int m;
private int n;
private int[][] grid;

public int numMagicSquaresInside(int[][] grid) {
m = grid.length;
n = grid[0].length;
this.grid = grid;
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += check(i, j);
}
}
return ans;
}

private int check(int i, int j) {
if (i + 3 > m || j + 3 > n) {
return 0;
}
int[] cnt = new int[16];
int[] row = new int[3];
int[] col = new int[3];
int a = 0, b = 0;
for (int x = i; x < i + 3; ++x) {
for (int y = j; y < j + 3; ++y) {
int v = grid[x][y];
if (v < 1 || v > 9 || ++cnt[v] > 1) {
return 0;
}
row[x - i] += v;
col[y - j] += v;
if (x - i == y - j) {
a += v;
}
if (x - i + y - j == 2) {
b += v;
}
}
}
if (a != b) {
return 0;
}
for (int k = 0; k < 3; ++k) {
if (row[k] != a || col[k] != a) {
return 0;
}
}
return 1;
}
}
```

### **C++**

```cpp
class Solution {
public:
int numMagicSquaresInside(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int ans = 0;
auto check = [&](int i, int j) {
if (i + 3 > m || j + 3 > n) {
return 0;
}
vector<int> cnt(16);
vector<int> row(3);
vector<int> col(3);
int a = 0, b = 0;
for (int x = i; x < i + 3; ++x) {
for (int y = j; y < j + 3; ++y) {
int v = grid[x][y];
if (v < 1 || v > 9 || ++cnt[v] > 1) {
return 0;
}
row[x - i] += v;
col[y - j] += v;
if (x - i == y - j) {
a += v;
}
if (x - i + y - j == 2) {
b += v;
}
}
}
if (a != b) {
return 0;
}
for (int k = 0; k < 3; ++k) {
if (row[k] != a || col[k] != a) {
return 0;
}
}
return 1;
};
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += check(i, j);
}
}
return ans;
}
};
```

### **Go**

```go
func numMagicSquaresInside(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
check := func(i, j int) int {
if i+3 > m || j+3 > n {
return 0
}
cnt := [16]int{}
row := [3]int{}
col := [3]int{}
a, b := 0, 0
for x := i; x < i+3; x++ {
for y := j; y < j+3; y++ {
v := grid[x][y]
if v < 1 || v > 9 || cnt[v] > 0 {
return 0
}
cnt[v]++
row[x-i] += v
col[y-j] += v
if x-i == y-j {
a += v
}
if x-i == 2-(y-j) {
b += v
}
}
}
if a != b {
return 0
}
for k := 0; k < 3; k++ {
if row[k] != a || col[k] != a {
return 0
}
}
return 1
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
ans += check(i, j)
}
}
return
}
```

### **TypeScript**

```ts
function numMagicSquaresInside(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const check = (i: number, j: number): number => {
if (i + 3 > m || j + 3 > n) {
return 0;
}
const cnt: number[] = new Array(16).fill(0);
const row: number[] = new Array(3).fill(0);
const col: number[] = new Array(3).fill(0);
let [a, b] = [0, 0];
for (let x = i; x < i + 3; ++x) {
for (let y = j; y < j + 3; ++y) {
const v = grid[x][y];
if (v < 1 || v > 9 || ++cnt[v] > 1) {
return 0;
}
row[x - i] += v;
col[y - j] += v;
if (x - i === y - j) {
a += v;
}
if (x - i === 2 - (y - j)) {
b += v;
}
}
}
if (a !== b) {
return 0;
}
for (let k = 0; k < 3; ++k) {
if (row[k] !== a || col[k] !== a) {
return 0;
}
}
return 1;
};
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
ans += check(i, j);
}
}
return ans;
}
```

### **...**
Expand Down
Loading