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. python3 获取阿里云ECS 实例及监控的方法

    #!/usr/bin/env python3.5 # -*- coding:utf8 -*- try: import httplib except ImportError: import http.c ...

  2. java http url post json

    import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import ja ...

  3. android studio 将library导出为jar 亲测成功

    本人使用的是helloChart这个开源项目,其主要用于图表,来自git 地址为:https://github.com/lecho/hellocharts-android 下载命令为 git clon ...

  4. moodle其他搜集

    1.将面包屑的符号换成">>",找到皮肤包里的config.php文件,在最后加入 $THEME->rarrow=">&gt"; ...

  5. eclipse中link方式安装插件

    今天需要给eclipse安装svn插件,觉得link方式便于管理于是就打算用这种方式来安装. 我电脑上的eclipse的安装目录是   E:\tools\eclipse   下面开始安装 1.在ecl ...

  6. C++ static与单例模式

    单例模式是应用最多的一种设计模式,它要求系统中每个类有且只能有一个实例对象. 主要优点: 1.提供了对唯一实例的受控访问. 2.由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创 ...

  7. Apache Storm简介

    Apache Storm简介 Storm是一个分布式的,可靠的,容错的数据流处理系统.Storm集群的输入流由一个被称作spout的组件管理,spout把数据传递给bolt, bolt要么把数据保存到 ...

  8. .net程序在无.net环境下运行

    看了篇文章  测试了下竟然真的可以运行  测试环境是XP下  没有装NET2.0的情况下 可以运行的   不过需要每次输入命令才能运行 点击后还是会报错 原文如下 众所周知,.net程序必须运行在.n ...

  9. tcp协议栈

    TCP/IP是互联网的核心协议,也是大多数网络应用的核心协议.就前面一段时间面试中问到的TCP/IP问题,这里给出一个简单的小结.   TCP由RFC793.RFC1122.RFC1323.RFC20 ...

  10. .NET 简单的扩展方法使用。

    写代码时,我们经常会碰到dll中提供的方法,不够用或者不好用的情况.而且我们也不方便去更改dll本身的源码. 这时候我们可以使用.NET提供的"扩展方法"去解决这个问题. 下面我写 ...