1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| class Solution { private final int[][] DIRECTIONS = new int[][] { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 }, { 0, -1 } };
public void gameOfLife(int[][] board) { for (int row = 0; row < board.length; row++) { for (int col = 0; col < board[0].length; col++) { int liveNeighbors = countLiveNeighbors(board, row, col); if (board[row][col] == 1) { board[row][col] = liveNeighbors < 2 ? -1 : liveNeighbors <= 3 ? 1 : -1; } else { board[row][col] = liveNeighbors == 3 ? -2 : 0; } } } for (int row = 0; row < board.length; row++) { for (int col = 0; col < board[0].length; col++) { if (board[row][col] == -1) { board[row][col] = 0; } else if (board[row][col] == -2) { board[row][col] = 1; } } } }
private int countLiveNeighbors(int[][] board, int row, int col) { int ans = 0; for (int[] direction : DIRECTIONS) { int newRow = row + direction[0]; int newCol = col + direction[1]; if (!isOutOfBound(board, newRow, newCol) && Math.abs(board[newRow][newCol]) == 1) { ans += 1; } } return ans; }
private boolean isOutOfBound(int[][] board, int row, int col) { return row < 0 || row >= board.length || col < 0 || col >= board[0].length; } }
|