-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.py
40 lines (40 loc) · 1.11 KB
/
Solution.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
32
33
34
35
36
37
38
39
40
#coding=utf-8
__author__ = 'xuxuan'
class Solution(object):
_board=[]
_rows=0
_cols=0
def gameOfLife(self, board):
"""
:type board: List[List[int]]
:rtype: void Do not return anything, modify board in-place instead.
"""
self._board=board
self._rows=len(board)
if self._rows!=0:
self._cols=len(board[0])
else:
return board
arrays=[0]*(self._rows*self._cols)
for i in range(len(arrays)):
m=i/self._cols
n=i%self._cols
num=self.f(m,n)
if num==3 or (num==2 and board[m][n]==1):
arrays[i]=1
for i in range(len(arrays)):
m=i/self._cols
n=i%self._cols
board[m][n]=arrays[i]
def f(self,m,n):
ans=0
for i in [-1,0,1]:
for j in [-1,0,1]:
if i!=0 or j!=0:
ans+=self.isLive(m+i,n+j)
return ans
def isLive(self,m,n):
if m<0 or n<0 or m>=self._rows or n>=self._cols:
return 0
else:
return self._board[m][n]