forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
33 lines (31 loc) · 1.18 KB
/
Solution.cs
File metadata and controls
33 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
namespace LeetCodeNet.G0201_0300.S0289_game_of_life {
// #Medium #Array #Matrix #Simulation #Top_Interview_150_Matrix
// #2025_07_16_Time_0_ms_(100.00%)_Space_46.31_MB_(66.67%)
public class Solution {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822", Justification = "LeetCode")]
public void GameOfLife(int[][] board) {
int m = board.Length, n = board[0].Length;
int[] dx = {0, 0, 1, 1, 1, -1, -1, -1};
int[] dy = {1, -1, 0, 1, -1, 0, 1, -1};
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int live = 0;
for (int d = 0; d < 8; d++) {
int ni = i + dx[d], nj = j + dy[d];
if (ni >= 0 && ni < m && nj >= 0 && nj < n && (board[ni][nj] & 1) == 1) live++;
}
if ((board[i][j] & 1) == 1) {
if (live == 2 || live == 3) board[i][j] |= 2;
} else {
if (live == 3) board[i][j] |= 2;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] >>= 1;
}
}
}
}
}