JAVA O(1) space, O(mn) time


  • 0
    F

    StefanPochmann‘s algorithm implement in java

    public class Solution {
        public void gameOfLife(int[][] board) {
            int n = board.length, m = n > 0 ? board[0].length : 0;
            for(int i =  0 ; i < n; ++i ){
                for (int j = 0 ; j < m ; ++j){
                    int lives = -board[i][j];
                     for (int x = Math.max(0,i-1); x < Math.min(i+2,n) ; ++x ){
                        for (int y = Math.max(0,j-1); y < Math.min(j+2,m) ;++y){
                            lives +=( board[x][y] & 0x01);
                        }
                    }
                    if ((lives|(board[i][j]& 0x01)) == 0x03)
                        board[i][j] |= 0x02;
                }
            }
            for(int i =  0 ; i < n; ++i )
                for (int j = 0; j < m; ++j)
                    board[i][j] >>= 1;
        }
    }
    

Log in to reply
 

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