Python, simple, no magic method


  • 0
    P

    class Solution(object):
    def evaluate(self, board, r, c):
    return sum(board[i][j] for i in range(r-1,r+2) for j in range(c-1, c+2)) - board[r][c]

    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])
        newboard = [[0] * (n+2) for i in range(m+2)]
        for i in range(1, m+1):
            for j in range(1, n+1):
                newboard[i][j] = board[i-1][j-1]
        for i in range(m):
            for j in range(n):
                check = self.evaluate(newboard, i+1, j+1)
                if board[i][j] == 1:
                    if check < 2 or check > 3:
                        board[i][j] = 0
                    else:
                        board[i][j] = 1
                elif board[i][j] == 0 and check == 3:
                    board[i][j] = 1

Log in to reply
 

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