forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
34 lines (34 loc) · 1.08 KB
/
Solution.cs
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
34
public class Solution {
public void GameOfLife(int[][] board) {
int m = board.Length;
int n = board[0].Length;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int live = -board[i][j];
for (int x = i - 1; x <= i + 1; ++x) {
for (int y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] > 0) {
++live;
}
}
}
if (board[i][j] == 1 && (live < 2 || live > 3)) {
board[i][j] = 2;
}
if (board[i][j] == 0 && live == 3) {
board[i][j] = -1;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 2) {
board[i][j] = 0;
}
if (board[i][j] == -1) {
board[i][j] = 1;
}
}
}
}
}