A coding practice.
class Solution { public: // encoding: 2 - 1 to 0; -1 - 0 to live void gameOfLife(vector<vector<int>>& board) { int h = board.size(); int w = board[0].size(); // Pass 1 for(int y = 0; y < h; y ++) for(int x = 0; x < w; x ++) { int cnt = 0; for(int dx = -1; dx < 2; dx ++) for(int dy = -1; dy < 2; dy ++) { int xx = x + dx, yy = y + dy; if (xx >=0 && xx < w && yy >= 0 && yy < h && !(xx==x && yy == y)) { cnt += board[yy][xx] > 0; } } if(board[y][x]) { if (!(cnt == 2 || cnt == 3)) { board[y][x] = 2; } } else { if(cnt == 3) board[y][x] = -1; } } // Pass 2 for(int y = 0; y < h; y ++) for(int x = 0; x < w; x ++) { if (board[y][x] == 2) board[y][x] = 0; else if (board[y][x] == -1) board[y][x] = 1; } } };