in place c solution.

• ``````/* state would like 00 01 10 11
* soluton 2, use 2 bits of per int to record state,
* first bit is old state, second bit is new state
* after state change, go through whole 2D matrix to
* use second bit to replace first bit
*/
#define OSTATE(v) (v & 1)
#define NSTATE(v, state) ((v & 1) + (state << 1))
#define GNSTATE(v) (v >> 1)
void nstate(int **board, int i, int j, int boardRowSize, int boardColSize)
{
int live = 0, state = board[i][j], ostate = state;
for (int m = i - 1; m <= i + 1; m ++) {
for (int n = j - 1; n <= j + 1; n++){
if(m < 0 || m >= boardRowSize ||
n < 0 || n >= boardColSize ||
(m == i && n == j)) {
continue;
}
OSTATE(board[m][n]) ? live ++ : 0;
}
}
state = live < 2 ? 0 : live == 2 ? state : live == 3 ? 1 : 0;
board[i][j] = NSTATE(ostate, state);
}

void gameOfLife(int** board, int boardRowSize, int boardColSize) {
for (int i = 0; i < boardRowSize; i ++) {
for (int j = 0; j < boardColSize; j++) {
nstate(board, i , j , boardRowSize, boardColSize);
}
}

for (int i = 0; i < boardRowSize; i ++) {
for (int j = 0; j < boardColSize; j ++) {
board[i][j] = GNSTATE(board[i][j]);
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.