0289.cpp (722B)
1 class Solution { 2 public: 3 void gameOfLife(vector<vector<int>> &board) { 4 int n = board.size(), m = board[0].size(); 5 for (int i = 0; i < n; i++) { 6 for (int j = 0; j < m; j++) { 7 int add = (board[i][j] & 1) << 1; 8 for (int k = max(i - 1, 0); k <= min(n - 1, i + 1); k++) { 9 for (int l = max(j - 1, 0); l <= min(m - 1, j + 1); l++) { 10 board[k][l] += add; 11 } 12 } 13 } 14 } 15 16 for (int i = 0; i < n; i++) { 17 for (int j = 0; j < m; j++) { 18 board[i][j] = board[i][j] == 7 || board[i][j] == 9 || board[i][j] == 6; 19 } 20 } 21 } 22 };