Skip to content

Instantly share code, notes, and snippets.

@bobfang1992
Created December 30, 2020 17:07
Show Gist options
  • Save bobfang1992/1bb2c80b649bf1acdee5d182f7f0c4d9 to your computer and use it in GitHub Desktop.
Save bobfang1992/1bb2c80b649bf1acdee5d182f7f0c4d9 to your computer and use it in GitHub Desktop.
Leetcode 289
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
m = len(board)
if m == 0:
return
n = len(board[0])
if n == 0:
return
for i in range(m):
for j in range(n):
count = 0
for r in range(max(i-1, 0), min(i+2, m)):
for c in range(max(j-1, 0), min(j+2, n)):
if ((i, j) != (r, c)) and abs(board[r][c]) == 1:
count += 1
if board[i][j] == 1:
if count not in [2, 3]:
board[i][j] = -1
if board[i][j] == 0:
if count == 3:
board[i][j] = 2
# for row in board:
# print(row)
for i in range(m):
for j in range(n):
if board[i][j] > 0:
board[i][j] = 1
else:
board[i][j] = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment