My Python Solution, 44ms


  • 0
    M
    def gameOfLife(self, board):
        """
        :type board: List[List[int]]
        :rtype: void Do not return anything, modify board in-place instead.
        """
        if len(board) == 0:
            return
        m, n = len(board), len(board[0])
        for row in xrange(m):
            for colum in xrange(n):
                counter = 0
                for i in xrange(row-1, row+2):
                    for j in xrange(colum-1, colum+2):
                        if 0 <= i < m and 0 <= j < n and board[i][j] in (1, 2):
                            counter += 1
                if board[row][colum] == 1:
                    counter -= 1
                    if counter < 2 or counter > 3:
                        board[row][colum] = 2
                elif board[row][colum] == 0:
                    board[row][colum] = 3 if counter == 3 else 0
        for row in xrange(m):
            for colum in xrange(n):
                board[row][colum] = 1 if board[row][colum] in (1, 3) else 0

Log in to reply
 

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