[LEETCODE][289. Game of Life][in-place方法,思想很好]
之前碰到的一题:
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?
题目意思也就是总共四种判断状态:
如果本身是1,有下面三种判断:
1.相连的8个方向中1的个数刚好是2或3,下一次为1
2.相连的8个方向中1的个数>3,下一次为0
3.相连的8个方向中1的个数<2,下一次为0
如果本身是0,有下面这种判断
4.如果相连的8个方向中1的个数刚好为3,下一次为1
O(mn)的空间复杂度,O(mn)的时间复杂度这个是好想的,需要开一个二维数组存储状态
那么对于in-place的方法:
原来的状态为1或者0,是用1bit来进行存储状态的,可以拓展成2bit,在最后进行右移1位即可。那我们进行考虑8个方向的判断,因为是同时进行更新,那么根据更新原则,判断条件为count == 3 || count - board[i][j] == 3。
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int m = board.size();
int n=board[0].size();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
int count=0;
for(int I=max(0,i-1);I<min(m,i+2);I++)
for(int J=max(0,j-1);J<min(n,j+2);J++)
count+=board[I][J]&1; //最低位和1与的结果
if(count==3 || count-board[i][j]==3)//在8个方向的判断过程中包括了当前节点本身,因此是上述的判断条件
board[i][j]|=2; //添加成2Bit来表示
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
board[i][j]>>=1; //最终右移一位更新
}
};
这个想法确实好,低位存之前的状态,高位存更新后的状态!!
- 这或许是对小白最友好的python入门了吧——11,if语句初体验
- 你的网站做好神马搜索优化了吗?神马搜索站长工具提交
- 应用程序的通信成本
- 这或许是对小白最友好的python入门了吧——18,定义函数
- 【深度学习】图片风格转换应用程序:使用CoreML创建Prisma
- 压力测试中存在的问题
- 这或许是对小白最友好的python入门了吧——18,用while循环处理列表
- 如何在js中将统计代码图标隐藏
- 警惕IT黑洞
- 这或许是对小白最友好的python入门了吧——17,while循环
- 泛型介绍(接上一篇,具体的事例随后呈上)
- PHP高级编程之守护进程
- 看到他我一下子就悟了-- 泛型(2)
- ExtJS4中设置tabpanel的tab高度问题
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- R语言使用bootstrap和增量法计算广义线性模型(GLM)预测置信区间
- Android 中两个Activity 之间的传值问题
- 学习使用Material Design控件(二)使用DrawerLayout实现侧滑菜单栏效果
- Android Spinner列表选择框的应用
- Android实现截屏方式整理(总结)
- 用RSHINY DASHBOARD可视化美国投票记录
- Android 侧滑关闭Activity的实例
- Android 两个Fragment之间的跳转和数据的传递实例详解
- Android编程实现保存图片到系统图库的方法示例
- Android自定义单选多选下拉列表的实例代码
- R语言POT超阈值模型在洪水风险频率分析中的应用研究
- Android开发之ToggleButton实现开关效果示例
- Android使用Circular Reveal动画让页面跳转更炫酷
- 学习使用Material Design控件(三)使用CardView实现卡片效果
- Android实现dialog的3D翻转示例