Java In-Place 1ms Solution


  • 0
    D
    public void gameOfLife(int[][] board) {
      int n = board.length;
      if (n == 0) {
        return;
      }
      int m = board[0].length;
    
      int[] drow = {-1, -1, -1, 0, 0, 1, 1, 1};
      int[] dcol = {-1, 0, 1, -1, 1, -1, 0, 1};
      for (int i = 0; i < n; i++) {
         for (int j = 0; j < m; j++) {
            int cnt = 0;
            for (int k = 0; k < 8; k++) {
                int row = i + drow[k];
                int col = j + dcol[k];
                if (row >= 0 && row < n && col >= 0 && col < m 
                    && (board[row][col] == 1 || board[row][col] == 2)) {
                    cnt++;
                }
            }
    
            if (board[i][j] == 0 && cnt == 3) {
                board[i][j] = 3;
            } else  if (board[i][j] == 1 && (cnt < 2 || cnt > 3)) {
                board[i][j] = 2;
            }
        }
      }
    
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            board[i][j] %= 2;
        }
      }
    }

Log in to reply
 

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