LeetCode – Game of Life (Java)

Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules:

Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population..
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state.

Java Solution 1

Because we need to solve the problem in place, we can use the higher bit to record the next state. And at the end, shift right a bit to get the next state for each cell.

public void gameOfLife(int[][] board) {
    if(board==null || board.length==0||board[0].length==0)
    int m=board.length;
    int n=board[0].length;
    int[] x = {-1, -1, 0, 1, 1, 1, 0, -1};
    int[] y = {0, 1, 1, 1, 0, -1, -1, -1};
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            int count=0;
            for(int k=0; k<8; k++){
                int nx=i+x[k];
                int ny=j+y[k];
            //<2 die
                board[i][j] &= 1;
            //same state
                board[i][j] |=  board[i][j]<<1;
            //go live
                board[i][j] |=2;
            //>3 die
                board[i][j] &=1;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            board[i][j] = board[i][j]>>1;
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. Mewanbanjop Mawroh on 2016-11-27

Leave a comment