-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path289.cpp
More file actions
27 lines (27 loc) · 928 Bytes
/
Copy path289.cpp
File metadata and controls
27 lines (27 loc) · 928 Bytes
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
class Solution {
public:
void gameOfLife(vector<vector<int>> &board) {
vector<vector<int>> ret(board);
int m = board.size(), n = m == 0 ? 0 : board[0].size();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int &&neighbour = 0;
for (int I = max(0, i - 1); I < min(m, i + 2); ++I) {
for (int J = max(0, j - 1); J < min(n, j + 2); ++J) {
if (I == i && J == j)
continue;
neighbour += board[I][J] & 1;
}
}
if (board[i][j] == 0) {
if (neighbour == 3)
ret[i][j] = 1;
} else {
if (neighbour < 2 || neighbour > 3)
ret[i][j] = 0;
}
}
}
board = ret;
}
};