Game of Life LEETCODE中档算法题JAVA实现

在LEETCODE上做这道题https://leetcode.com/problems/game-of-life/ 

在DISCUSS讨论中看到了惊为天人的答案,不需要额外的空间,巧妙利用二进制,教科书般值得背诵的数组边界控制。

 用二进制储存状态,0代表死,1代表活,第二位代表下一代的死活状态,第一位代表当代的死活状态

 00 代表下一代死 当代死 

01代表下一代死 当代活 

10代表下一代活,当代死 

11代表下一代活,这一代活 

这样做的好处是不用额外的空间来储存和更新数据,直接对原二进制操作,board[i][j] & 1就可以获得当代状态 

a&b中的&是二目操作符,先把a,b转换成二进制,然后对每一位考虑,若都为1,则为1,否则为0

具体看上面的四个数据,00 & 1 ,结果是0,当代死;01&1 ,结果是1,当代活;10&1,结果实11&1,结果是01,当代活; 

为得到下一代数据,我们用 board[i][j] >>1 来忽略第一位,直接得到下一代数据。



</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">public void gameOfLife(int[][] board) {
    if(board == null || board.length == 0) return;
    int m = board.length, n = board[0].length;

    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            int lives = liveNeighbors(board, m, n, i, j);

            // In the beginning, every 2nd bit is 0;
            // So we only need to care about when 2nd bit will become 1.
            if((board[i][j] & 1) == 1 && (lives >= 2 && lives <= 3)) {
                board[i][j] = 3; // Make the 2nd bit 1: 01 ---> 11
            }
            if((board[i][j] & 1) == 0 && lives == 3) {
                board[i][j] = 2; // Make the 2nd bit 1: 00 ---> 10
            }
        }
    }

    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            board[i][j] >>= 1;  // Get the 2nd state.
        }
    }
}

public int liveNeighbors(int[][] board, int m, int n, int i, int j) {
    int lives = 0;
    for(int p = Math.max(i - 1, 0); p <= Math.min(i + 1, m - 1); p++) {
        for(int q = Math.max(j - 1, 0); q <= Math.min(j + 1, n - 1); q++) {
            lives += board[p][q] & 1;
        }
    }
    lives -= board[i][j] & 1;
    return lives;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值