[抄题]:

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. [R] t.test()

    t.test(x, y = NULL, alternative = c("two.sided", "less","greater"), mu ...

  2. HDU5542 The Battle of Chibi

    题意 给出长度为n的序列,问这个序列中有多少个长度为m的单调递增子序列. \(1\le M\le N\le 1000\) 分析 用F[i,j]表示前j个数构成以Aj为结尾的数列中,长度为i的严格递增子 ...

  3. 建筑的永恒之道 (C·亚历山大 著)

    永恒之道 建筑或城市只有踏上了永恒之道,才会生机勃勃. 第1章 永恒之道 它是一个唯有我们自己才能带秩序的过程,它不可能被求取,但只要我们顺应它,它便会自然而然地出现. 质 为了探求永恒之道,我们首先 ...

  4. Entity Framework教程翻译 ---- 系列教程

    Entity Framework教程(第二版) (翻译)Entity Framework技巧系列之十四 - Tip 56 (翻译)Entity Framework技巧系列之十三 - Tip 51 - ...

  5. Python小练习(一)

    1:有一个列表,其中包括10个元素,例如这个列表是[1,2,3,4,5,6,7,8,9,0],要求将列表中的每个元素一次向前移动一个位置,第一个元素到列表的最后,然后输出这个列表.最终样式是[2,3, ...

  6. 1.3 Linux分区类型

    1.主分区最多只能有4个. 2.扩展分区: 最多只能有一个: 主分区加扩展分区最多只能有4个: 扩展分区只能包含逻辑分区,不能写数据. 3.格式化目的: 写入文件系统:清除数据:划出文件分配表(i n ...

  7. 2017 browser market share

    Refer to Net Market Share published data for year 2017, browser share percentage as below table show ...

  8. jenkins 可以设置最多执行并发执行多少个

    系统-系统配置

  9. Altium Designer9.4局域网内冲突的问题

    Altium Designer破解 1.安装Altium Designer原程序.2.运行AD9KeyGen,点击“打开模板”,加载ad9.ini,如想修改注册名,只需修改:TransactorNam ...

  10. vim常用操作和使用技巧

    vi是linux与unix下的常用文本编辑器,其运行稳定,使用方便,本文将分两部分对其常用操作技巧和配置进行阐述,其中参考了网上的一些文章,对作者表示感谢 PART1 操作技巧 说明: 以下的例子中  ...