Python Solution


  • -1
    T
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        m, n = len(board), len(board[0])
        for j in range(m):
            for i in range(n):
                lives = sum(1 & board[j + dj][i + di]
                            for dj in (-1, 0, 1) for di in (-1, 0, 1)
                            if 0 <= j + dj < m and 0 <= i + di < n
                            ) - board[j][i]
                if board[j][i] and 1 < lives < 4:
                    board[j][i] = 3
                elif not board[j][i] and lives == 3:
                    board[j][i] = 2
        for j in range(m):
            for i in range(n):
                board[j][i] >>= 1

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.