LeetCode.289.Game of Life 生命游戏
题目描述
289 Game of Life
https://leetcode-cn.com/problems/game-of-life/
According to the Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”
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 (taken from the above Wikipedia article):
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. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.
Example:
Input:
[
[0,1,0],
[0,0,1],
[1,1,1],
[0,0,0]
]
Output:
[
[0,0,0],
[1,0,1],
[0,1,1],
[0,1,0]
]
Follow up:
1、Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
2、In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches the border of the array. How would you address these problems?
解题过程
用数组中不存在的值保存下次状态
遍历二维数组,统计每个元素周围 1 的个数,由于要求 in place 原地操作,把 1 变 0 的记为 -1, 把 0 变 1 的记为 2,最后再遍历一次恢复。
时间复杂度 O(mn)
,空间复杂度 O(1)
private static class SolutionV2020 {
private int[] dx = {-1, -1, 0, 1, 1, 1, 0, -1}, dy = {0, 1, 1, 1, 0, -1, -1, -1};
public void gameOfLife(int[][] board) {
if (null == board || board.length == 0) {
return;
}
int nx = board.length, ny = board[0].length;
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
int lives = countAroundLives(board, i, j);
if (1 == board[i][j] && (lives < 2 || lives > 3)) {
board[i][j] = -1; // 1变0设为-1
} else if (0 == board[i][j] && lives == 3){
board[i][j] = 2; // 0变1设为2
}
}
}
// 复位 -1 -> 0, 2 -> 1
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (board[i][j] == -1) {
board[i][j] = 0;
} else if (board[i][j] == 2) {
board[i][j] = 1;
}
}
}
}
// 统计 i,j 周围 1 的个数
private int countAroundLives(int[][] board, int i, int j) {
int nx = board.length, ny = board[0].length;
int count = 0;
for (int k = 0; k < 8; k++) {
int ii = i + dx[k], jj = j + dy[k];
if (ii >= 0 && ii < nx && jj >= 0 && jj < ny && 1 == Math.abs(board[ii][jj])) {
count++;
}
}
return count;
}
}
用空闲bit位保存下次状态
还有一种很好的思路,使用位运算,题目只用到了 0 和 1 ,也就是一个 32 位 int 的最低位,我们可以使用 int 的倒数第二位来保存下一次的状态,然后最后再统一右移一位即可。
GitHub代码
algorithms/leetcode/leetcode/_289_GameOfLife.java
https://github.com/masikkk/algorithms/blob/master/leetcode/leetcode/_289_GameOfLife.java
上一篇 LeetCode.008.String to Integer (atoi) 字符串转换整数
下一篇 JUnit
页面信息
location:
protocol
: host
: hostname
: origin
: pathname
: href
: document:
referrer
: navigator:
platform
: userAgent
: