【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 ...
随机推荐
- [笔记]SciPy、Matplotlib基础操作
NumPy.SciPy.Matplotlib,Python下机器学习三大利器.上一篇讲了NumPy基础操作,这节讲讲SciPy和Matplotlib.目前接触到的东西不多,以后再遇到些比较常用的再更新 ...
- Fail-Fast机制详解
Java中的Iterator非常方便地为所有的数据源提供了一个统一的数据读取(删除)的接口,但是在使用的时候容易报如下错误ConcurrentModificationException,原因是在使用迭 ...
- Centos5搭建vsftpd服务
更换镜像源 由于centos5已经历史久远,内置的镜像源已经不能用.看: 因此,我手工更换了阿里云的源.(ps:我本来是想用网易的源,但不知为什么,这个源在安装vsftpd时提示http 404错误) ...
- 基于java:读写一个英文的txt文件,记录单词个数,并输出十个出现最多的单词及出现的个数;
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; class W ...
- Debian系统简要说明
Debian这个是我最喜欢也是比较熟悉的一个系统了,BD下做个简要说明 一,APT以及dpkg常见用法如下:功能具体语句 软件源设置 /etc/apt/sources.list 更新软件源数据 ...
- 关于List<T> 的排序
/** * @author hjn * @entity Student * @date 2017年5月23日15:22:18 */ public class Student { private Str ...
- 日志组件二:log4j2
一.背景 随着业务服务(Server App)逐渐增加,我们的业务系统中的日志输出面临的问题越来越多,高并发下对磁盘io这块消耗的越来越大,因此,急需要一个高性能且最好能够支持异步输出日志的日志框架, ...
- 你对SpringMvc是如何理解的?
SpringMVC工作原理 SpringMvc是基于过滤器对servlet进行了封装的一个框架,我们使用的时候就是在web.xml文件中配置DispatcherServlet类:SpringMvc工作 ...
- winfrom中将panel另存为图片
private void button1_Click(object sender, EventArgs e) { Point ScrollMaxInfo = Get ...
- Hadoop集群搭建(非HA)
1.准备Linux环境 1.0先将虚拟机的网络模式选为NAT 1.1修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=itcast ### ...