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):

  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by over-population..
  4. 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:

  1. 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.
  2. 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的更多相关文章

  1. leetcode@ [289] Game of Life (Array)

    https://leetcode.com/problems/game-of-life/ According to the Wikipedia's article: "The Game of ...

  2. [LeetCode] 289. Game of Life 生命游戏

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  3. LeetCode 289. Game of Life (生命游戏)

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  4. Java实现 LeetCode 289 生命游戏

    289. 生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有 ...

  5. LeetCode 289. Game of Life (C++)

    题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...

  6. Leetcode 289.生命游戏

    生命游戏 根据百度百科,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在1970年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细胞.每个细胞具有一个初始状 ...

  7. leetcode 289生命游戏

    class Solution { public: vector<vector<,},{,},{,},{,-},{,-},{-,-},{-,},{-,}}; void gameOfLife( ...

  8. LeetCode | 289. 生命游戏(原地算法/位运算)

    记录dalao的位运算骚操作 根据百度百科 ,生命游戏,简称为生命,是英国数学家约翰·何顿·康威在 1970 年发明的细胞自动机. 给定一个包含 m × n 个格子的面板,每一个格子都可以看成是一个细 ...

  9. 2017-3-9 leetcode 283 287 289

    今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...

随机推荐

  1. web.xml讲解

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "- ...

  2. find用法积累

    查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xar ...

  3. 修改searchBar的返回按钮的显示文字

    #pragma mark 搜索框的代理方法,搜索输入框获得焦点(聚焦) - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { ...

  4. syntaxhighlighter的使用

    第一 解压压缩包,scripts文件夹中包含了各种语言的JS文件,在styles文件夹中是各种显示高亮的主题 第二 如何使用?首先要引入其核心javascript文件shCore.js和核心CSS文件 ...

  5. Python 实现类似PHP的strip_tags函数功能,并且可以自定义设置保留标签

    最近在研究 Python ,发现用的还是很不习惯,很多PHP里面很简单的功能在Python 里面都得找半天,而且很多功能都得自己实现. 今天做个采集,需要过滤内容中的标签,搞了一下午,貌似终于搞出来了 ...

  6. PHP控制连接打印机

    一.需求 使用PHP控制连接打印机 现场实时连续打印动态数据 二.配置 php运行环境正确安装(Apache|Nginx + PHP) 下载与php版本对应的php_printer.dll扩展 扩展文 ...

  7. Python数据预处理—归一化,标准化,正则化

    关于数据预处理的几个概念 归一化 (Normalization): 属性缩放到一个指定的最大和最小值(通常是1-0)之间,这可以通过preprocessing.MinMaxScaler类实现. 常用的 ...

  8. markdown 自定义一个锚点

    //自定义锚点 s "m[": function mlink( text ) { var orig = String(text); // Inline content is pos ...

  9. 查询--游标 limit skip sort

    打印出所有的里程: var cursor = db.tblDaily.find(); cursor.forEach(function(x){ print(x.DailyCount + x.DailyU ...

  10. [APP]如果你想反编译

    反编译,主要用到两类工具,一个就是获取apk包的包名(appPackage)和类名(appActivity)的工具,其实就是反编译出java源代码,dex2jar和jd-gui:一个是将一个apk包反 ...