【LeetCode】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.
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?
提示:
由于题目中要求所有点的状态需要一次性发生改变,而且不用额外的空间,这是本题的最大难点。
既然需要“就地解决”,我们不妨分析一下borad的特性:board上的元素有两种状态,生(1)和死(0)。这两种状态存在了一个int型里面。所以我们可以有效利用除最低位的其它位,去保存更新后的状态,这样就不需要有额外的空间了。
具体而言,我们可以用最低位表示当前状态,次低位表示更新后状态:
- 00(0):表示当前是死,更新后是死;
- 01(1):表示当前是生,更新后是死;
- 10(2):表示当前是死,更新后是生;
- 11(3):表示当前是神,更新后是生。
代码:
class Solution {
public:
void gameOfLife(vector<vector<int>>& board) {
int height = board.size();
int width = height ? board[].size() : ;
if (!height || !width) {
return;
}
for (int i = ; i < height; ++i) {
for (int j = ; j < width; ++j) {
int life = getlives(board, height - , width - , i, j);
if (board[i][j] == && (life == || life == )) {
board[i][j] = ;
} else if (board[i][j] == && life == ) {
board[i][j] = ;
}
}
}
for (int i = ; i < height; ++i) {
for (int j = ; j < width; ++j) {
board[i][j] >>= ;
}
}
}
int getlives(vector<vector<int>>& board, int height, int width, int i, int j) {
int res = ;
for (int h = max(i-, ); h <= min(i+, height); ++h) {
for (int w = max(j-, ); w <= min(j+, width); ++w) {
res += board[h][w] & ;
}
}
res -= board[i][j] & ;
return res;
}
};
【LeetCode】289. Game of Life的更多相关文章
- 【LeetCode】289. Game of Life 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- mysql语句优化总结(一)
Sql语句优化和索引 1.Innerjoin和左连接,右连接,子查询 A. inner join内连接也叫等值连接是,left/rightjoin是外连接. SELECT A.id,A.nam ...
- dedecms学习笔记
终于弄懂了dedecms的架构和原理,然后搭建了人生中的第一个网站.网站名就不说了. dede的后台在dede中,这是后台代码 templets/default中放的是模板 article 里是文章内 ...
- 自己写的书《深入理解Android虚拟机内存管理》,不出版只是写着玩
百度网盘地址:https://pan.baidu.com/s/1jI4xZgE 我给起的书名叫做<深入理解Android虚拟机内存管理>.本书分为两个部分,前半部分主要是我对Linux0. ...
- SPRING AOP ....0 can't find referenced pointcut
下载最新的aspectjweaver就可以了,因为JDK的版本的问题不兼容. //织入点语法 @Pointcut("execution(public * com.frank.dao..*.* ...
- git常用命令记录
配置本地仓库 git config --global user.name.git config --global user.email 查看配置详情 git config -l 初始仓库 git in ...
- 使用FileUtils简化你的文件操作
前言: 在工作当中我们往往遇到很多文件的操作,我们也习惯写一些自己定义的工具类来简化文件操作,其实apache的commons的FileUtils类就是这样一个工具类,使用它能大大的简化我们对文件的操 ...
- Js判断是否是直接进入本页面的
今天带来一个Js的小示例,用来判断当前页面的链接来路.很多人应该可以用到,这个虽然非常简单,但是用到的地方却还是挺多的 首先新建一个index.html,代码如下 <!DOCTYPE html& ...
- javascript常见面试题
闭包相关面试题:1. var a=0,b=0; function A(a){ A=function(b){console.log(a+b++);}; console.log(a); } A(1); A ...
- 在.NET Core中使用Irony实现自己的查询语言语法解析器
在之前<在ASP.NET Core中使用Apworks快速开发数据服务>一文的评论部分,.NET大神张善友为我提了个建议,可以使用Compile As a Service的Roslyn为语 ...
- iOS地理围栏技术的应用
遇到一个需求,要求监测若干区域,设备进入这些区域则要上传数据,且可以后台监测,甚至app被杀死也要监测.发现oc的地理围栏技术完美匹配这个需求,任务做完了,把遇到的坑记录下来,也许能帮到你呢. 要做这 ...