-
Notifications
You must be signed in to change notification settings - Fork 0
/
week6 289. Game of Life
32 lines (30 loc) · 982 Bytes
/
week6 289. Game of Life
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
public class Solution {
public void gameOfLife(int[][] board) {
int m = board.length;
int n = board[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int num = getLivNum(board,i,j);
if(board[i][j] == 0 && num == 3) board[i][j]+=10;
if(board[i][j] == 1 && ((num == 2)||(num == 3))) board[i][j]+=10;
}
}
for(int i = 0; i<m; i++){
for(int j = 0; j < n; j++){
board[i][j]/=10;
}
}
}
public int getLivNum(int[][] board, int x, int y){
int m = board.length;
int n = board[0].length;
int count = 0;
for(int i = x-1; i<=x+1;i++){
for(int j = y-1; j<= y+1; j++){
if(i<0 || i>=m || j<0 || j>=n || (i==x && j== y)) continue;
if(board[i][j]%10 == 1) count++;
}
}
return count;
}
}