-
Notifications
You must be signed in to change notification settings - Fork 0
/
289. Game of Life.java
51 lines (50 loc) · 1.45 KB
/
289. Game of Life.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public void gameOfLife(int[][] board) {
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
board[i][j] = getAlive(board, i, j);
}
}
for (int i = 0; i < board.length; ++i) {
for (int j = 0; j < board[0].length; ++j) {
if (board[i][j] == -1) {
board[i][j] = 1;
} else if (board[i][j] == -2) {
board[i][j] = 0;
}
}
}
}
public int getAlive(int[][] board, int x, int y) {
int alive = 0;
int self = board[x][y];
for (int i = x - 1; i < x + 2; ++i) {
for (int j = y - 1; j < y + 2; ++j) {
if (i == x && j == y) {
continue;
}
if (i < 0 || j < 0 || i > board.length - 1 || j > board[0].length - 1) {
continue;
}
if (board[i][j] != -1 && board[i][j] != 0) {
alive++;
}
}
}
if (self == 1) {
if (alive < 2 || alive > 3) {
// 1 -> 0
return -2;
} else {
return 1;
}
} else {
if (alive == 3) {
// 0 -> 1
return -1;
} else {
return 0;
}
}
}
}