forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
35 lines (35 loc) · 1.01 KB
/
Solution.ts
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
/**
Do not return anything, modify board in-place instead.
*/
function gameOfLife(board: number[][]): void {
const m = board.length;
const n = board[0].length;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
let live = -board[i][j];
for (let x = i - 1; x <= i + 1; ++x) {
for (let y = j - 1; y <= j + 1; ++y) {
if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] > 0) {
++live;
}
}
}
if (board[i][j] === 1 && (live < 2 || live > 3)) {
board[i][j] = 2;
}
if (board[i][j] === 0 && live === 3) {
board[i][j] = -1;
}
}
}
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (board[i][j] === 2) {
board[i][j] = 0;
}
if (board[i][j] === -1) {
board[i][j] = 1;
}
}
}
}