A/C Python solution, easy to understand, beat 7% -_-


  • 0
    W

    class Solution(object):
    def gameOfLife(self, board):
    """
    :type board: List[List[int]]
    :rtype: void Do not return anything, modify board in-place instead.
    """

        #print "board = ", board
    
        numRow = len(board)
        #print "numRow = ", numRow
    
        if (numRow < 1):
            return
    
        numCol = len(board[0])
        #print "numCol = ", numCol
    
        #newBoard = [[0]*numCol] * numRow
    
        #rowList = []
        newBoard = []
        for r in range(numRow):
            for c in range(numCol):
                newBoard.append(0)
        #print "newBoard = ", newBoard
    
        # for c in range(numCol):
        #     newBoard.append(newBoard[0])
        # print "newBoard = ", newBoard
    
        indexList = []
    
        for r in range(numRow):
            for c in range(numCol):
                #print "r = ", r, " c = ", c, " board[r][c] = ", board[r][c]
                indexList.append((r, c))
    
    
        for r in range(numRow):
            for c in range(numCol):
                livNum = 0
    
                if (r-1, c-1) in indexList and board[r-1][c-1] == 1:
                    livNum +=1
    
                if (r-1, c) in indexList and board[r-1][c] == 1:
                    livNum +=1
    
                if (r-1, c+1) in indexList and board[r-1][c+1] == 1:
                    livNum +=1
    
                if (r, c-1) in indexList and board[r][c-1] == 1:
                    livNum +=1
    
                if (r, c+1) in indexList and board[r][c+1] == 1:
                    livNum +=1
    
                if (r+1, c-1) in indexList and board[r+1][c-1] == 1:
                    livNum +=1
    
                if (r+1, c) in indexList and board[r+1][c] == 1:
                    livNum +=1
    
                if (r+1, c+1) in indexList and board[r+1][c+1] == 1:
                    livNum +=1
    
                #print "now start case, newBoard = ", newBoard
                #print "r = ", r, " c = ", c
    
                if board[r][c] == 1:
                    #print " == 1"
                    if livNum < 2:
                        #print "case 1"
                        newBoard[r*numCol + c] = 0
                    elif livNum == 2 or livNum == 3:
                        #print "case 2"
                        #print "before case 2 , newBoard = ", newBoard
                        newBoard[r*numCol + c] = 1
                        #print "after case 2, newBoard = ", newBoard
                    elif livNum > 3:
                        #print "case 3"
                        newBoard[r*numCol + c] = 0
                else:
                    #print " == 0"
                    if board[r][c] == 0:
                        #print " == 0 -2", " newBoard[r][c] = ", newBoard[r*numCol + c]
                        if livNum == 3:
                            #print "case 4"
                            newBoard[r*numCol + c] = 1
    
                #print " livNum = ", livNum, " board[r][c] = ", board[r][c], " newBoard[r*numRow + c] = ", newBoard[r*numCol + c]
    
    
    
        #print "newBoard = ", newBoard
        for r in range(numRow):
            for c in range(numCol):
                board[r][c] = newBoard[r*numCol + c]
        #print "after update, board = ", board
        return

Log in to reply
 

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