LeetCode 题解工作台

生命游戏

根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态: 1 即为 活细胞 (live),或 0 即为 死细胞 (dead)。每个细胞与其八个相…

category

3

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·matrix

bolt

答案摘要

我们不妨定义两个新的状态,其中状态 表示活细胞在下一个状态转为死细胞,状态 表示死细胞在下一个状态转为活细胞。那么,对于当前遍历到的格子,如果格子大于 ,就表示当前格子是活细胞,否则就是死细胞。 因此,我们可以遍历整个面板,对于每个格子,统计其周围的活细胞数目,用变量 表示。如果当前格子是活细胞,那么当 $live \lt 2$ 或者 $live \gt 3$ 时,当前格子的下一个状态是死细…

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·matrix 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

根据 百度百科 , 生命游戏 ,简称为 生命 ,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机。

给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞。每个细胞都具有一个初始状态: 1 即为 活细胞 (live),或 0 即为 死细胞 (dead)。每个细胞与其八个相邻位置(水平,垂直,对角线)的细胞都遵循以下四条生存定律:

  1. 如果活细胞周围八个位置的活细胞数少于两个,则该位置活细胞死亡;
  2. 如果活细胞周围八个位置有两个或三个活细胞,则该位置活细胞仍然存活;
  3. 如果活细胞周围八个位置有超过三个活细胞,则该位置活细胞死亡;
  4. 如果死细胞周围正好有三个活细胞,则该位置死细胞复活;

下一个状态是通过将上述规则同时应用于当前状态下的每个细胞所形成的,其中细胞的出生和死亡是 同时 发生的。给你 m x n 网格面板 board 的当前状态,返回下一个状态。

给定当前 board 的状态,更新 board 到下一个状态。

注意 你不需要返回任何东西。

 

示例 1:

输入:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
输出:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

示例 2:

输入:board = [[1,1],[1,0]]
输出:[[1,1],[1,1]]

 

提示:

  • m == board.length
  • n == board[i].length
  • 1 <= m, n <= 25
  • board[i][j]01

 

进阶:

  • 你可以使用原地算法解决本题吗?请注意,面板上所有格子需要同时被更新:你不能先更新某些格子,然后使用它们的更新后的值再更新其他格子。
  • 本题中,我们使用二维数组来表示面板。原则上,面板是无限的,但当活细胞侵占了面板边界时会造成问题。你将如何解决这些问题?
lightbulb

解题思路

方法一:原地标记

我们不妨定义两个新的状态,其中状态 22 表示活细胞在下一个状态转为死细胞,状态 1-1 表示死细胞在下一个状态转为活细胞。那么,对于当前遍历到的格子,如果格子大于 00,就表示当前格子是活细胞,否则就是死细胞。

因此,我们可以遍历整个面板,对于每个格子,统计其周围的活细胞数目,用变量 livelive 表示。如果当前格子是活细胞,那么当 live<2live \lt 2 或者 live>3live \gt 3 时,当前格子的下一个状态是死细胞,即状态 22;如果当前格子是死细胞,那么当 live=3live = 3 时,当前格子的下一个状态是活细胞,即状态 1-1

最后,我们再遍历一遍面板,将状态 22 的格子更新为死细胞,将状态 1-1 的格子更新为活细胞。

时间复杂度 O(m×n)O(m \times n),其中 mmnn 分别是面板的行数和列数。我们需要遍历整个面板。空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
    def gameOfLife(self, board: List[List[int]]) -> None:
        m, n = len(board), len(board[0])
        for i in range(m):
            for j in range(n):
                live = -board[i][j]
                for x in range(i - 1, i + 2):
                    for y in range(j - 1, j + 2):
                        if 0 <= x < m and 0 <= y < n and board[x][y] > 0:
                            live += 1
                if board[i][j] and (live < 2 or live > 3):
                    board[i][j] = 2
                if board[i][j] == 0 and live == 3:
                    board[i][j] = -1
        for i in range(m):
            for j in range(n):
                if board[i][j] == 2:
                    board[i][j] = 0
                elif board[i][j] == -1:
                    board[i][j] = 1
speed

复杂度分析

指标
时间complexity is O(m _n) for both passes over the board, as each cell is visited a constant number of times to check neighbors. Space complexity can be O(1) if using in-place encoding, or O(m_ n) if creating a separate matrix for the next state.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Are you handling neighbor counts without modifying the board prematurely?

  • question_mark

    Can you optimize the solution to avoid extra space while maintaining simultaneous updates?

  • question_mark

    Do you understand why marking states in-place prevents incorrect state transitions?

warning

常见陷阱

外企场景
  • error

    Updating cells in a single pass without markers causes neighbor counts to be incorrect.

  • error

    Forgetting to check all eight neighbors, leading to off-by-one errors on edges.

  • error

    Using extra space unnecessarily when in-place marking is possible.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Implement the Game of Life on an infinite grid where new cells can appear dynamically.

  • arrow_right_alt

    Compute k steps of the Game of Life efficiently without recomputing the entire board each time.

  • arrow_right_alt

    Optimize neighbor checks using prefix sums or bit manipulation for larger matrices.

help

常见问题

外企场景

生命游戏题解:数组·matrix | LeetCode #289 中等