-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGameofLife.java
More file actions
33 lines (31 loc) · 1.13 KB
/
GameofLife.java
File metadata and controls
33 lines (31 loc) · 1.13 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
public class Solution {
public void gameOfLife(int[][] board) {
int m = board.length, n = board[0].length;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int numOfNeighbors = neighbors(board, i, j);
if (board[i][j] == 0 && numOfNeighbors == 3) {
board[i][j] = 3;
} else if (board[i][j] == 1 && (numOfNeighbors < 2 || numOfNeighbors > 3)) {
board[i][j] = 2;
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
board[i][j] %= 2;
}
}
}
public int neighbors(int[][] board, int i, int j) {
int numberOfLives = 0;
for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1, board.length - 1); ++x) {
for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1, board[0].length - 1); ++y) {
if ((x != i || y != j) && (board[x][y] == 2 || board[x][y] == 1)) {
numberOfLives++;
}
}
}
return numberOfLives;
}
}