Skip to content

Commit 15d0220

Browse files
authored
feat: add solutions to lc problem: No.0840 (#1371)
1 parent c5291a1 commit 15d0220

File tree

8 files changed

+690
-1
lines changed

8 files changed

+690
-1
lines changed

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

+238
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,260 @@
4949

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

52+
**方法一:枚举**
53+
54+
我们直接枚举每个 $3 \times 3$ 子矩阵的左上角坐标 $(i, j)$,然后判断该子矩阵是否满足“幻方矩阵”,若是,答案加一。枚举结束后,返回答案即可。
55+
56+
时间复杂度 $O(m \times n)$,其中 $m$ 和 $n$ 分别是矩阵的行数和列数。空间复杂度 $O(1)$。
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

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

5864
```python
65+
class Solution:
66+
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
67+
def check(i: int, j: int) -> int:
68+
if i + 3 > m or j + 3 > n:
69+
return 0
70+
s = set()
71+
row = [0] * 3
72+
col = [0] * 3
73+
a = b = 0
74+
for x in range(i, i + 3):
75+
for y in range(j, j + 3):
76+
v = grid[x][y]
77+
if v < 1 or v > 9:
78+
return 0
79+
s.add(v)
80+
row[x - i] += v
81+
col[y - j] += v
82+
if x - i == y - j:
83+
a += v
84+
if x - i == 2 - (y - j):
85+
b += v
86+
if len(s) != 9 or a != b:
87+
return 0
88+
if any(x != a for x in row) or any(x != a for x in col):
89+
return 0
90+
return 1
5991

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

6296
### **Java**
6397

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

66100
```java
101+
class Solution {
102+
private int m;
103+
private int n;
104+
private int[][] grid;
105+
106+
public int numMagicSquaresInside(int[][] grid) {
107+
m = grid.length;
108+
n = grid[0].length;
109+
this.grid = grid;
110+
int ans = 0;
111+
for (int i = 0; i < m; ++i) {
112+
for (int j = 0; j < n; ++j) {
113+
ans += check(i, j);
114+
}
115+
}
116+
return ans;
117+
}
118+
119+
private int check(int i, int j) {
120+
if (i + 3 > m || j + 3 > n) {
121+
return 0;
122+
}
123+
int[] cnt = new int[16];
124+
int[] row = new int[3];
125+
int[] col = new int[3];
126+
int a = 0, b = 0;
127+
for (int x = i; x < i + 3; ++x) {
128+
for (int y = j; y < j + 3; ++y) {
129+
int v = grid[x][y];
130+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
131+
return 0;
132+
}
133+
row[x - i] += v;
134+
col[y - j] += v;
135+
if (x - i == y - j) {
136+
a += v;
137+
}
138+
if (x - i + y - j == 2) {
139+
b += v;
140+
}
141+
}
142+
}
143+
if (a != b) {
144+
return 0;
145+
}
146+
for (int k = 0; k < 3; ++k) {
147+
if (row[k] != a || col[k] != a) {
148+
return 0;
149+
}
150+
}
151+
return 1;
152+
}
153+
}
154+
```
155+
156+
### **C++**
157+
158+
```cpp
159+
class Solution {
160+
public:
161+
int numMagicSquaresInside(vector<vector<int>>& grid) {
162+
int m = grid.size();
163+
int n = grid[0].size();
164+
int ans = 0;
165+
auto check = [&](int i, int j) {
166+
if (i + 3 > m || j + 3 > n) {
167+
return 0;
168+
}
169+
vector<int> cnt(16);
170+
vector<int> row(3);
171+
vector<int> col(3);
172+
int a = 0, b = 0;
173+
for (int x = i; x < i + 3; ++x) {
174+
for (int y = j; y < j + 3; ++y) {
175+
int v = grid[x][y];
176+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
177+
return 0;
178+
}
179+
row[x - i] += v;
180+
col[y - j] += v;
181+
if (x - i == y - j) {
182+
a += v;
183+
}
184+
if (x - i + y - j == 2) {
185+
b += v;
186+
}
187+
}
188+
}
189+
if (a != b) {
190+
return 0;
191+
}
192+
for (int k = 0; k < 3; ++k) {
193+
if (row[k] != a || col[k] != a) {
194+
return 0;
195+
}
196+
}
197+
return 1;
198+
};
199+
for (int i = 0; i < m; ++i) {
200+
for (int j = 0; j < n; ++j) {
201+
ans += check(i, j);
202+
}
203+
}
204+
return ans;
205+
}
206+
};
207+
```
208+
209+
### **Go**
210+
211+
```go
212+
func numMagicSquaresInside(grid [][]int) (ans int) {
213+
m, n := len(grid), len(grid[0])
214+
check := func(i, j int) int {
215+
if i+3 > m || j+3 > n {
216+
return 0
217+
}
218+
cnt := [16]int{}
219+
row := [3]int{}
220+
col := [3]int{}
221+
a, b := 0, 0
222+
for x := i; x < i+3; x++ {
223+
for y := j; y < j+3; y++ {
224+
v := grid[x][y]
225+
if v < 1 || v > 9 || cnt[v] > 0 {
226+
return 0
227+
}
228+
cnt[v]++
229+
row[x-i] += v
230+
col[y-j] += v
231+
if x-i == y-j {
232+
a += v
233+
}
234+
if x-i == 2-(y-j) {
235+
b += v
236+
}
237+
}
238+
}
239+
if a != b {
240+
return 0
241+
}
242+
for k := 0; k < 3; k++ {
243+
if row[k] != a || col[k] != a {
244+
return 0
245+
}
246+
}
247+
return 1
248+
}
249+
for i := 0; i < m; i++ {
250+
for j := 0; j < n; j++ {
251+
ans += check(i, j)
252+
}
253+
}
254+
return
255+
}
256+
```
257+
258+
### **TypeScript**
67259

260+
```ts
261+
function numMagicSquaresInside(grid: number[][]): number {
262+
const m = grid.length;
263+
const n = grid[0].length;
264+
const check = (i: number, j: number): number => {
265+
if (i + 3 > m || j + 3 > n) {
266+
return 0;
267+
}
268+
const cnt: number[] = new Array(16).fill(0);
269+
const row: number[] = new Array(3).fill(0);
270+
const col: number[] = new Array(3).fill(0);
271+
let [a, b] = [0, 0];
272+
for (let x = i; x < i + 3; ++x) {
273+
for (let y = j; y < j + 3; ++y) {
274+
const v = grid[x][y];
275+
if (v < 1 || v > 9 || ++cnt[v] > 1) {
276+
return 0;
277+
}
278+
row[x - i] += v;
279+
col[y - j] += v;
280+
if (x - i === y - j) {
281+
a += v;
282+
}
283+
if (x - i === 2 - (y - j)) {
284+
b += v;
285+
}
286+
}
287+
}
288+
if (a !== b) {
289+
return 0;
290+
}
291+
for (let k = 0; k < 3; ++k) {
292+
if (row[k] !== a || col[k] !== a) {
293+
return 0;
294+
}
295+
}
296+
return 1;
297+
};
298+
let ans = 0;
299+
for (let i = 0; i < m; ++i) {
300+
for (let j = 0; j < n; ++j) {
301+
ans += check(i, j);
302+
}
303+
}
304+
return ans;
305+
}
68306
```
69307

70308
### **...**

0 commit comments

Comments
 (0)