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?
思路:要求 in-place ,所以要区分三种变化状态 1->0,1->1,0->1,将三种状态分别用-1/-2/-3三个状态值来表示
第一次遍历时,先判断当前cell的转换状态,且将cell值置为对应状态值,在后面的遍历中根据状态值则可推断出原始值,进而判断转换状态。
第二次遍历,则将状态值置为变换后的值,注意一下在每个cell遍历其周围八个点时用的循环,是怎么排除边界情况的
public class S289 {
public void gameOfLife(int[][] board) {
int m = board.length,n = board[0].length;
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
int liveCount = 0;
for (int k = i-1;k <= i+1;k++) {
for (int l = j-1;l <= j+1;l++) {
if(k < 0 || l < 0 || k > m-1 || l > n-1 || (k == i && l == j)) continue;
liveCount += getRealNum(board[k][l]);
}
}
if (board[i][j] == 1) {
if (liveCount <2 || liveCount >3 ) {
board[i][j] = -1;
} else {
board[i][j] = -2;
}
} else {
if (liveCount == 3) {
board[i][j] = -3;
}
}
}
}
for (int i = 0;i < m;i++) {
for (int j = 0;j < n;j++) {
if (board[i][j] == -2 || board[i][j] == -3)
board[i][j] = 1;
else if (board[i][j] == -1)
board[i][j] = 0;
}
}
}
public static int getRealNum(int i){
if(i == -1 || i == -2)
return 1;
else if (i == -3) {
return 0;
}
return i;
}
public static void main(String[] args) {
S289 s = new S289();
int [][]b = {{1}};
s.gameOfLife(b);
}
}
Leetcode 289 Game of Life的更多相关文章
- leetcode@ [289] Game of Life (Array)
https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...
- [LeetCode] 289. Game of Life 生命游戏
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- LeetCode 289. Game of Life (生命游戏)
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- Java实现 LeetCode 289 生命游戏
289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...
- LeetCode 289. Game of Life (C++)
题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...
- Leetcode 289.生命游戏
生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...
- leetcode 289生命游戏
class Solution { public: vector<vector<,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; void gameOfLife( ...
- LeetCode | 289. 生命游戏(原地算法/位运算)
记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...
- 2017-3-9 leetcode 283 287 289
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
随机推荐
- asp.net mvc 上传附件验证
1.使用验证特性 [RequiredCollection] public ICollection<IFormFile> Attachments { get; set; } 2.自定义验证特 ...
- 用ES6语法和方式写gulp
安装依赖模块 npm i -g gulp npm i gulp babel-core babel-preset-es2015 --save-dev 在创建文件 .babelrc(文件名) : (文件内 ...
- WPF中override ResourceDictionary中的设置的方法
当资源文件里改变了控件的样式时,在使用的地方如果想改变资源文件里修改的内容,会造成无法达到预期目的的结果. 以DataGrid为例,我在资源文件里,改变了默认的DataGrid的样式,其中我设置了Is ...
- Android 简单的图片缩放方法
很简单的一个图片缩放方法,注意要比例设置正确否则可能会内存溢出 相关问题 java.lang.IllegalArgumentException: bitmap size exceeds 32bits ...
- 使用curl来调试你的应用
我们在客户端开发过程中总免不了和后端进行api对接,有时候需要对返回的数据格式进行调试,有时候每次运行客户端来发送请求,这个未免效率太低,这里就来介绍一个好用的工具--curl. curl curl是 ...
- Scala 中的 apply 和 update 方法[转]
原文链接:http://blog.csdn.net/lyrebing/article/details/21696581 Scala 是构建在 JVM 上的静态类型的脚本语言,而脚本语言总是会有些约定来 ...
- 【Python】考虑用生成器改写直接返回列表的函数
使用生成器的好处是显而易见的,可以使代码更加清晰,同时减小内存的消耗,当函数需要返回列表,把函数改写为生成器是相对容易的. 下面这两个函数返回字符串中每个单词的索引: def index_words1 ...
- PortMon(电脑开放端口检查工具) 3.03 免费绿色版
软件名称: PortMon(电脑开放端口检查工具) 3.03 免费绿色版 软件语言: 英文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP / Win2 ...
- Webstrom 常用操作记录
WebStorm 编译 es6 与 scss 的教程: http://blog.jetbrains.com/webstorm/2015/05/ecmascript-6-in-webstorm-tran ...
- 通过mvn archetype:generate创建Maven项目模板慢的问题
通过mvn archetype:generate这种交互方式来创建Maven项目模板的时候,经常会长时间卡在Generating project in Interactive mode这一行提示(图1 ...