Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def gameOfLife(self, board: 'List[List[int]]') -> 'None':
- """
- Do not return anything, modify board in-place instead.
- """
- # create the direction vector
- dx = [-1, -1, -1, 0, 0, 1, 1, 1]
- dy = [-1, 0, 1, -1, 1, -1, 0, 1]
- for i in range(0, len(board)): # go through each cell in the board
- for j in range(0, len(board[0])):
- live = 0 # accumulate the live state cell
- for d in range(0, 8): # go through surrounding 8 cells
- x = i + dx[d]
- y = j + dy[d]
- if x > -1 and x < len(board) and y > -1 and y < len(board[0]): # houlding the boundary cases
- if board[x][y] == 1 or board[x][y] == 2: # if the surrounding cell is live accumulate the live number
- live += 1
- if board[i][j] == 1: # calculate the state of the current cell
- if live < 2 or live > 3:
- board[i][j] = 2
- elif board[i][j] == 0:
- if live == 3:
- board[i][j] = 3
- for i in range(0, len(board)):
- for j in range(0, len(board[0])):
- board[i][j] = board[i][j] % 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement