打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
289  LeetCode Game of Life

原题链接在这里:https://leetcode.com/problems/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):

  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?

题解:

简单的想法就是copy 原来矩阵,然后根据copy 来修改原来的矩阵,如此做会使用O(m*n) space.

如何做到In-space呢,我们需要根据改动前的状态来数出live cell 的个数,已经更改的点如何知道原有的状态呢。就要多mark出几种conditions.

Dead->Dead: Condition 0; Live->Live : Condition 1; Live->Dead: Condition 2; Dead->Live:Condition 3

如此在数数的时候如果原道1 和 2他们原有的状态都是live,就都要算。

最后通过把所有的数%2来得到更改后的状态。

 

如何定义这四种状态?第一个原因是通过如此顺序是因为0本身就是dead cell, 1本身就是live cell, 如此在countLive完成后可以尽量少改动数组,如果还是live的1的状态就不用动了;

第二个原因最后对0,1,2,3改回0,1时 可以直接通过%2改动省事。

如果需要记住原有状态就可以通过增加condition 来搞定。

Time Complexity: O(m*n). Space: O(1).

AC Java:

 1 public class Solution { 2     public void gameOfLife(int[][] board) { 3         if(board == null || board.length == 0 || board[0].length == 0){ 4             return; 5         } 6         int m = board.length; 7         int n = board[0].length; 8         //Mark four conditions 9         // Dead->Dead: 0; Live->Live : 1; Live->Dead: 2; Dead->Live:310         for(int i = 0; i<m; i++){11             for(int j = 0; j<n; j++){12                 int count = getLive(board, i, j);   //计算周围8个位置上原来有多少个live cell13                 if(board[i][j] == 0 && count == 3){ //dead -> live14                     board[i][j] = 3;15                 }else if(board[i][j] == 1 && (count < 2 || count > 3)){ //live -> dead16                     board[i][j] = 2;17                 }18             }19         }20         //Seconde iteration, mark 2 to 0, mark 3 to 1.21         for(int i = 0; i<m; i++){22             for(int j = 0; j<n; j++){23                 board[i][j] = board[i][j]%2;24             }25         }26     }27     //计算周围8个位置上有多少个原来是live 的 细胞28     private int getLive(int [][] board, int i, int j){29         int count = 0;30         for(int x = i-1; x<=i+1; x++){31             for(int y = j-1; y<=j+1; y++){32                 if(x<0 || x>=board.length || y<0 || y>=board[0].length || (x == i && y == j)){33                     continue;34                 }35                 if(board[x][y] == 1 || board[x][y] == 2){36                     count++;37                 }38             }39         }40         return count;41     }42 }

 

标签: LeetCodeArray
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
[LeetCode] Game of Life 生命游戏
UV/448可固定活性染色试剂盒,专为光谱流设计
字节二面原题,有效的数独
生命游戏(头条、Google 面试题)
硬度计|力传感器闭环系统Closed loop sys/双语
拼图游戏算法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服