【LeetCode】289. Game of Life 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/game-of-life/description/
题目描述
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:
- 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.
- 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?
题目大意
玩一个生存游戏。这个游戏给了一个二维数组,每个数组上写的是这个地方是否有部落。看每个位置的 8 连通位置的部落数:
- 如果一个活着的部落,其周围少于2个部落,这个部落会死;
- 如果一个活着的部落,其周围部落数在2或者3,这个部落活到下一个迭代中;
- 如果一个活着的部落,其周围多于3个部落,这个部落会死;
- 如果一个死了的部落,其周围多于3个部落,这个部落会活。
一次迭代是同时进行的,求一轮之后,整个数组。
解释下 4 联通和 8 联通:
解题方法
方法很简单暴力,直接统计每个位置的 8 连通分量并根据部落数进行题目所说的判断就好了。
由于一次迭代是同时进行的,所以不能一边统计一边修改数组,这样会影响后面的判断。我的做法是先把输入的面板复制了一份,这样使用原始的 board
去判断部落数,更新放在了新的 board_next
上不会影响之前的 board
。最后再把数值复制过来。
题目给了 4 个存活和死亡的判断条件,直接按照这4个条件判断即可。我定义了一个函数liveOrDead()
用来判断当前判断的部落应该活还是死,返回结果的解释:0-不变, 1-活下来,2-要死了。
时间复杂度是O(MN),空间复杂度是O(MN).
Python代码如下:
class Solution(object):
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
if board and board[0]:
M, N = len(board), len(board[0])
board_next = copy.deepcopy(board)
for m in range(M):
for n in range(N):
lod = self.liveOrDead(board, m, n)
if lod == 2:
board_next[m][n] = 0
elif lod == 1:
board_next[m][n] = 1
for m in range(M):
for n in range(N):
board[m][n] = board_next[m][n]
def liveOrDead(self, board, i, j):# return 0-nothing,1-live,2-dead
ds = [(1, 1), (1, -1), (1, 0), (-1, 1), (-1, 0), (-1, -1), (0, 1), (0, -1)]
live_count = 0
M, N = len(board), len(board[0])
for d in ds:
r, c = i + d[0], j + d[1]
if 0 <= r < M and 0 <= c < N:
if board[r][c] == 1:
live_count += 1
if live_count < 2 or live_count > 3:
return 2
elif board[i][j] == 1 or (live_count == 3 and board[i][j] ==0):
return 1
else:
return 0
参考资料:
https://leetcode.com/problems/game-of-life/discuss/73223/Easiest-JAVA-solution-with-explanation/178496
日期
2018 年 9 月 22 日 —— 周末加班
【LeetCode】289. Game of Life 解题报告(Python)的更多相关文章
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
随机推荐
- linux下定位异常消耗的线程实战分析
前言: 之前分享过一篇Linux开发coredump文件分析实战分享 ,今天再来分享一篇实战文章. 在我们嵌入式linux开发过程中,开发过程中我们经常会使用多进程.多线程开发.那么多线程使用过程中, ...
- 20. VIM命令操作技巧
V可视化选中当前行,根据光标可多行 ctrl+v 可视化块 v可视化根据光标 行间移动 快速增删改查 d 0 删除当前位置到行首 d $ 删除当前位置到行尾 d t (" ] ) )符号 ...
- 03-Collection用例管理及批量执行
当我们对一个或多个系统中的很多用例进行维护时,首先想到的就是对用例进行分类管理,同时还希望对这批用例做回归测试 .在postman也提供了这样一个功能,就是Collection .通过这个Collec ...
- 4.3 rust func closure
fn add_one_v1 (x: u32) -> u32 { x + 1 } let add_one_v2 = |x: u32| -> u32 { x + 1 }; let add_on ...
- AI ubantu 环境安装
ubantu安装记录 apt install python3-pip anaconda安装 https://repo.anaconda.com/archive/Anaconda3-2020.11-Li ...
- Linux基础命令---exportfs管理挂载的nfs文件系统
exportfs exportfs主要用于管理当前NFS服务器的文件系统. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 /usr/sb ...
- 【Python】【Module】json and pickle
Python中用于序列化的两个模块 json 用于[字符串]和 [python基本数据类型] 间进行转换 pickle 用于[python特有的类型] 和 [python基本数据类型]间进 ...
- maven依赖对zookeeper的版本冲突问题
我用的是springcloudAlibaba+zookeeper zookeeper下载后 1,修改配置文件,conf目录下的zoo_sample.cfg修改为zoo.cfg. 2,打开zoo.cfg ...
- 【Spark】【RDD】从HDFS创建RDD
1.在HDFS根目录下创建目录(姓名学号) hdfs dfs -mkdir /zwj25 hdfs dfs -ls / 访问 http://[IP]:50070 2.上传本地文件到HDFS hdfs ...
- Java中的循环结构(二)
循环结构(二) 学习本章有道的单词: rate:速度,比率 young:年轻的,年少 schedule:时间表,调度 neggtive:消极的;否定 customer:顾客,观众 birthday:生 ...