Two pass python solution


  • 0
    Z
    class Solution(object):
        def gameOfLife(self, board):
            """
            :type board: List[List[int]]
            :rtype: void Do not return anything, modify board in-place instead.
            """
            for i in range(len(board)):
                for j in range(len(board[0])):
                    l = 0 if j==0 or board[i][j-1]==3 or board[i][j-1]==0 else 1;
                    r = 0 if j==len(board[0])-1 or board[i][j+1]==3 or board[i][j+1]==0 else 1;
                    u = 0 if i==0 or board[i-1][j]==3 or board[i-1][j]==0 else 1;
                    d = 0 if i==len(board)-1 or board[i+1][j]==3 or board[i+1][j]==0 else 1;
                    lu = 0 if j==0 or i==0 or board[i-1][j-1]==3 or board[i-1][j-1]==0 else 1;
                    ru = 0 if i==0 or j==len(board[0])-1 or board[i-1][j+1]==3 or board[i-1][j+1]==0 else 1;
                    ld = 0 if j==0 or i==len(board)-1 or board[i+1][j-1]==3 or board[i+1][j-1]==0 else 1;
                    rd = 0 if j==len(board[0])-1 or i==len(board)-1 or board[i+1][j+1]==3 or board[i+1][j+1]==0 else 1;
                    sum_val = l + r+u+d+lu+ld+ru+rd
                    if board[i][j]==1:
                        if sum_val<2 or sum_val>3:
                        # 1->0: 2
                            board[i][j]=2
                    if board[i][j]==0:
                        if sum_val==3:
                        # 0->1: 3
                            board[i][j]=3
            for i in range(len(board)):
                for j in range(len(board[0])):
                    if board[i][j]==2:
                        board[i][j]=0
                    if board[i][j]==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.