289. 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:
- 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?
 
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:

[思维问题]:
[英文数据结构或算法,为什么不用别的数据结构或算法]:
&& (count == 3)要加括号扩起来
[一句话思路]:
用cur next两个二进制位和board[][]总数 来共同表示邻居的数量
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 自己另外写的新函数要写外面啊,不要写里面了
 - 统计数量的时候,默认都是0,只需要写next是1的复活情况,其他的不用写
 
[二刷]:
- int函数记得要写return
 - 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数组生存游戏的更多相关文章
- Python学习笔记  之  递归、二维数组顺时针旋转90°、正则表达式
		
递归.二维数组顺时针旋转90°.正则表达式 1. 递归算法是一种直接或间接调用自身算法的过程. 特点: 递归就是在过程或函数里调用自身 明确的递归结束条件,即递归出口 简洁,但是不提倡 递归次数多 ...
 - js 数组去重 的5种方法
		
一万数组,4个重复项,先贴上成绩. 1.3毫秒 2.115毫秒 3.71毫秒 4.6毫秒 1.哈希表 2.JQuery (最快的方法是用JQuery 这句话是截图带的... 实际上Jq是最慢的) 3. ...
 - OC省字典的数组摘要集
		
开放式党员 NSString *filePath = @"/Users/dlios/Downloads/area.txt"; 推断错误值 打印出来 NSError *error = ...
 - 如何用人工的方式将Excel里的一堆数字变成一个数组
		
目的是抛砖引玉,有谁可以教教我如何吧Excle的数据导入MyEclipse么? 如果只有⑨个字符的话我肯定是直接人工输入的,然而这次有65536行乘以3组,遭不住啊. 一.数组之间要有逗号在B列右键, ...
 - Numpy系列(十)- 掩码数组
		
简介 有时候数据集中存在缺失.异常或者无效的数值,我们可以标记该元素为被屏蔽(无效)状态. import numpy as np import numpy.ma as ma x = np.array( ...
 - C#编程(五十七)----------位数组
		
位数组 如果需要处理很多位,就可以使用BitArray类和BitVector32.BitArray位于命名空间System.Collections中. BitVector32位于命名空间System. ...
 - 2017-3-9 leetcode 283 287 289
		
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
 - LeetCode刷题总结-数组篇(中)
		
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
 - (二)初识NumPy库(数组的操作和运算)
		
本章主要介绍的是ndarray数组的操作和运算! 一. ndarray数组的操作: 操作是指对数组的索引和切片.索引是指获取数组中特定位置元素的过程:切片是指获取数组中元素子集的过程. 1.一维数组的 ...
 
随机推荐
- ARTIFICIAL INTELLIGENCE FOR GAMES (Ian Millington / John Funge 著)
			
相关网站:http://www.ai4g.com PART I AI AND GAMESCHAPTER1 INTRODUCTIONCHAPTER2 GAME AIPART II TECHNIQUESC ...
 - dos2unix 批量转化文件
			
在windows和linux双平台下开发,同时也用git作为同步工具,但前期没有注意,导致很多文件使用windows下的换行符CRLF 参考资料了解dos2unix可以转化格式. 但有个问题,虽然可以 ...
 - create table 推荐规则
			
create table 推荐规则: 所有列都设置NOT NULL,都写备注(comment) 除主键外,所有列都设置默认值(default)
 - 解决centos 7.5安装openvpn,mirrors.163.com提示没有可用软件包openvpn、easy-rsa问题
			
提示: yum install openvpn 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirro ...
 - Aria2+百度网盘  无限制的下载神器
			
Aria2是一款免费开源跨平台且不限速的多线程下载软件,Aria2的优点是速度快.体积小.资源占用少:支持 HTTP / FTP / BT / Magnet 磁力链接等类型的文件下载:支持 Win.M ...
 - babelrc 中的 presets 字段(env, react)和 plugins 字段(dynamic-import-webpack, transform-object-rest-spread, ...)
			
一.presets 字段 目前用到 presets: [ 'env', 'react' // react 转码规则 ]: 只有 env 时,作用和 latest 相同,包括 es5.es6.es7 ...
 - [UE4]Spin Box,数字输入,可拖动
			
一.Spin Box在Input组下 二.Spin Box的文字样式可以在Spin Box.Display中修改 三.Spin Box事件 1.On Value Changed:值改变时触发 2.On ...
 - Windows下,python  pip安装时ReadTimeoutError解决办法
			
一般情况下PIP出现ReadTimeoutError都是因为被GFW给墙了,所以一般遇到这种问题,我们可以选择国内的镜像来解决问题. 在Windows下: C:\Users\Administrator ...
 - 采用link方式解决zabbix对于备份监控和ORACLE日志监控由于路径不统一的问题
			
#对于备份监控和ORACLE日志监控由于路径不统一,我们可以采用link的方式如:#ln -s 原路径 新路径(/zabbix/logs)#新路径统一放在/zabbix/logs下具体看模板指定. # ...
 - 换上 SansForgetica-Regular 字体,增加记忆能力
			
最近澳大利亚的RMIT(皇家墨尔本理工大学) 搞出来这么个字体,号称能增强记忆,原理是通过难以识别的字体,让人提起精神去识别,从而记忆更深刻. 果断弄了个试试. 安装过程: 下载字体文件 点这里去下载 ...