public class Solution { public void GameOfLife(int[][] board) { var row = board.GetLength(0) - 1; var col = board[0].GetLength(0) - 1; var list = new List<List<int>>(); for (int r = 0; r <= row; r++) { var l = new List<int>(); for (int c = 0; c <= col; c++) { l.Add(board[r][c]); } list.Add(l); } for (int r = 0; r <= row; r++) { for (int c = 0; c <= col; c++) { int livecount = 0; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) { continue; } var newx = r + i; var newy = c + j; if (newx < 0 || newx > row || newy < 0 || newy > col) { continue; } if (board[newx][newy] == 1) { livecount++;//活节点 } } } if (board[r][c] == 1)//当前节点是活节点 { if (livecount == 2 || livecount == 3) { list[r][c] = 1; } else { list[r][c] = 0; } } else { if (livecount == 3) { list[r][c] = 1; } } } } for (int i = 0; i < list.Count; i++) { for (int j = 0; j < list[0].Count; j++) { board[i][j] = list[i][j]; } } } }