[抄题]:

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):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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?

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

&& (count == 3)要加括号扩起来

[一句话思路]:

用cur next两个二进制位和board[][]总数 来共同表示邻居的数量

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 自己另外写的新函数要写外面啊,不要写里面了
  2. 统计数量的时候,默认都是0,只需要写next是1的复活情况,其他的不用写

[二刷]:

  1. int函数记得要写return
  2. m*n行的时候,index只能=到n-1 比如3*3 只能到012

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

用cur next两个二进制位和board[][]总数 来共同表示邻居的数量

[复杂度]:Time complexity: O(mn) Space complexity: O(1)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

class Solution {
public void gameOfLife(int[][] board) {
//initialization
int m = board.length;
int n = board[0].length; //corner case
if (board == null || m == 0 || n == 0) return; //discuss the alive situations
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int c = countNeighbor(board, m, n, i, j);
if (board[i][j] == 0 && (c == 3)) board[i][j] = 2;
if (board[i][j] == 1 && (c == 2 || c == 3)) board[i][j] = 3;
}
} //remove the current state by >>
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
board[i][j] >>= 1;
}
}
} public int countNeighbor(int[][] board, int m, int n, int i, int j) {
//add count in 4 directions
int count = 0;
for (int x = Math.min(0, i - 1); x <= Math.min(m - 1, i + 1); x++) {
for (int y = Math.min(0, j - 1); y <= Math.min(n - 1, j + 1); y++) {
count += board[x][y] & 1;
}
} //remove the current state
count -= board[i][j] & 1;
return count;
}
}

[潜台词] :

289. Game of Life数组生存游戏的更多相关文章

  1. Python学习笔记 之 递归、二维数组顺时针旋转90°、正则表达式

    递归.二维数组顺时针旋转90°.正则表达式 1.   递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...

  2. js 数组去重 的5种方法

    一万数组,4个重复项,先贴上成绩. 1.3毫秒 2.115毫秒 3.71毫秒 4.6毫秒 1.哈希表 2.JQuery (最快的方法是用JQuery 这句话是截图带的... 实际上Jq是最慢的) 3. ...

  3. OC省字典的数组摘要集

    开放式党员 NSString *filePath = @"/Users/dlios/Downloads/area.txt"; 推断错误值 打印出来 NSError *error = ...

  4. 如何用人工的方式将Excel里的一堆数字变成一个数组

    目的是抛砖引玉,有谁可以教教我如何吧Excle的数据导入MyEclipse么? 如果只有⑨个字符的话我肯定是直接人工输入的,然而这次有65536行乘以3组,遭不住啊. 一.数组之间要有逗号在B列右键, ...

  5. Numpy系列(十)- 掩码数组

    简介 有时候数据集中存在缺失.异常或者无效的数值,我们可以标记该元素为被屏蔽(无效)状态. import numpy as np import numpy.ma as ma x = np.array( ...

  6. C#编程(五十七)----------位数组

    位数组 如果需要处理很多位,就可以使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System. ...

  7. 2017-3-9 leetcode 283 287 289

    今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. (二)初识NumPy库(数组的操作和运算)

    本章主要介绍的是ndarray数组的操作和运算! 一. ndarray数组的操作: 操作是指对数组的索引和切片.索引是指获取数组中特定位置元素的过程:切片是指获取数组中元素子集的过程. 1.一维数组的 ...

随机推荐

  1. 【java多线程】队列系统之ArrayBlockingQueue源码

    1.简介 ArrayBlockingQueue,顾名思义:基于数组的阻塞队列.数组是要指定长度的,所以使用ArrayBlockingQueue时必须指定长度,也就是它是一个有界队列. 它实现了Bloc ...

  2. 使用rpm 打包开发的postgres extension

      环境准备 安装依赖包 rpmdevtools rpm-build yum install -y rpm-build rpmdevtools 初始化rpm pacakge 项目 主要是rpm 打包的 ...

  3. Linux useful commands

    cat misc. cat xxx | more cat xxx | less cat > xxx , create a file xxx cat -n xxx | more with line ...

  4. py-day2-5 python 百分号字符串拼接

    #### 字符串格式化. # %s 代替任何的元素 (数字,字符串,列表··) print('I live %s crty' %'my') print('I live %s crty' %'[6,8, ...

  5. Linux cp命令详解

    Linux cp命令 Linux cp命令主要用于复制文件或目录,将源文件复制至目标文件,或将多个源文件复制至目标目录 用法: cp [选项]... [-T] 源文件 目标文件 cp [选项]... ...

  6. Eclipse配置Python的IDE

    我第一个用来实际应用的编程语言是Java,于是对Eclipse情有独钟.但是自从上手了Notepad++后,使用Eclipse的机会越来越少. 最近开始学习Python,因为对Python不太熟悉,有 ...

  7. JAVA常用工具类异常处理

    1异常的定义 异常就是与我们编译相违背在过程中出现的逻辑或忘记一些赋值等等 分为编译时错误和运行时错误 运行时异常 我们一般处理的时Exception异常: 异常处理 异常处理可以通过关键字try,c ...

  8. win7文件夹带锁标志如何去除?win7去除文件夹带锁标志的方法

    win7文件夹带锁标志如何去除?win7去除文件夹带锁标志的方法 http://www.xitongcheng.com/jiaocheng/win7_article_30333.html 具体方法如下 ...

  9. html/css/js-如何利用jq来更改属性的值和获取属性的值

    jquery的使用在web开发中是非常广泛的,虽然说比较容易,易学,但在开发过程中,也总是会碰到各种各样的小问题. 我曾经就遇到这种问题,jq如何获取属性值和更改属性值的. 众所周知,attr()可以 ...

  10. 变式配置简介 VARIANT CONFIGURATION

    变式物料类型KMAT; ITEM CATEGORY GROUP: main item 0002 sub item 0004 strategy group:25 requirement type: ke ...