-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path289.cpp
More file actions
22 lines (22 loc) · 750 Bytes
/
Copy path289.cpp
File metadata and controls
22 lines (22 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
// Runtime: 4 ms, faster than 83.08% of C++ online submissions for Game of
// Life. Memory Usage: 8.5 MB, less than 97.14% of C++ online submissions for
// Game of Life.
public:
void gameOfLife(vector<vector<int>> &board) {
int m = board.size(), n = m ? board[0].size() : 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int count = 0;
for (int I = max(i - 1, 0); I < min(i + 2, m); ++I)
for (int J = max(j - 1, 0); J < min(j + 2, n); ++J)
count += board[I][J] & 1;
if (count == 3 || count - board[i][j] == 3)
board[i][j] |= 2;
}
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
board[i][j] >>= 1;
}
};