Created
March 6, 2016 01:43
-
-
Save lulalachen/66ef6c2864adbb98a723 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution(object): | |
def gameOfLife(self, board): | |
""" | |
:type board: List[List[int]] | |
:rtype: void Do not return anything, modify board in-place instead. | |
""" | |
answer = copy.deepcopy(board) | |
row = len(board) | |
col = len(board[0]) | |
def check_rule(cur, live): | |
return (cur == 1 and live == 2) or (cur == 1 and live == 3) or (cur == 0 and live == 3) | |
def is_not_at_border(m, n): | |
return (m >= 0) and (n >= 0) and (m < row) and (n < col) | |
def check_neighbors(m, n): | |
live = 0 - answer[m][n] # remove self live or dead | |
for i in range(-1, 2): | |
for j in range(-1, 2): | |
if (is_not_at_border(m+i, n+j) and answer[m+i][n+j] == 1): | |
live += 1 | |
# print(m, n, live) | |
return int(check_rule(answer[m][n], live)) | |
for i in range(0, row): | |
for j in range(0, col): | |
board[i][j] = check_neighbors(i, j) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment