-
Notifications
You must be signed in to change notification settings - Fork 481
/
0289.py
31 lines (25 loc) · 978 Bytes
/
0289.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
r, c = len(board), len(board[0])
neighbors = [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (1, 1), (-1, 1), (1, -1)]
for i in range(r):
for j in range(c):
lives = 0
for m, n in neighbors:
if 0 <= i + m < r and 0 <= j + n < c:
lives += board[i+m][j+n] & 1
if board[i][j] and 2 <= lives < 4:
board[i][j] = 3
if not board[i][j] and lives == 3:
board[i][j] = 2
for i in range(r):
for j in range(c):
board[i][j] >>= 1
if __name__ == "__main__":
board = [[1,1],[1,0]]
Solution().gameOfLife(board)
print(board)