-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path289.js
31 lines (31 loc) · 822 Bytes
/
289.js
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
var gameOfLife = function(board) {
var neighborNum = (r,c)=>{
let num=0;
for(let i=r-1;i<=r+1;i++){
if(i<0)continue;
if(i>board.length-1)break;
for(let j=c-1;j<=c+1;j++){
if(i==r&&j==c)continue;
if(j<0)continue;
if(j>board[i].length-1)break;
num+=board[i][j]&1;
}
}
return num;
}
board.forEach((row,r)=>{
for(let i=0;i<row.length;i++){
let num = neighborNum(r,i);
if(row[i]===0&&num===3){
row[i] = 2;
}else if(row[i]===1&&num<=3&&num>=2){
row[i]=3;
}
}
})
board.forEach((row)=>{
for(let i=0;i<row.length;i++){
row[i]>>=1;
}
})
};