Skip to content

Commit af81a75

Browse files
committed
update: 289
1 parent 5a587be commit af81a75

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
8383
|274|[H-Index](https://leetcode.com/problems/h-index/) | [JavaScript](./src/h-index/res.js)|Medium|
8484
|275|[H-Index II](https://leetcode.com/problems/h-index-ii/) | [JavaScript](./src/h-index-ii/res.js)|Medium|
8585
|287|[Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) | [JavaScript](./src/find-the-duplicate-number/res.js)|Medium|
86+
|289|[Game of Life](https://leetcode.com/problems/game-of-life/) | [JavaScript](./src/game-of-life/res.js)|Medium|
8687
|299|[Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [JavaScript](./src/bulls-and-cows/res.js)|Medium|
8788
|307|[Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [JavaScript](./src/range-sum-query-mutable/res.js)|Medium|
8889
|309|[Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/) | [JavaScript](./src/best-time-to-buy-and-sell-stock-with-cooldown/res.js)|Medium|

src/game-of-life/res.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[][]} board
3+
* @return {void} Do not return anything, modify board in-place instead.
4+
*/
5+
var gameOfLife = function(board) {
6+
const n = board.length;
7+
const m = board[0].length;
8+
const flag = board.map(col => col.map(() => 0));
9+
10+
// Flag
11+
for (let i = 0; i < n; i++) {
12+
for (let j = 0; j < m; j++) {
13+
const ele = board[i][j];
14+
if (ele) {
15+
for (let tbIndex = Math.max(i-1, 0); tbIndex < Math.min(i+2, n); tbIndex++) {
16+
for (let lrIndex = Math.max(j-1, 0); lrIndex < Math.min(j+2, m); lrIndex++) {
17+
if (tbIndex === i && lrIndex === j) continue;
18+
flag[tbIndex][lrIndex]++;
19+
}
20+
}
21+
// console.log('new flag', flag)
22+
}
23+
}
24+
}
25+
26+
// console.log(flag);
27+
for (let i = 0; i < n; i++) {
28+
for (let j = 0; j < m; j++) {
29+
if (board[i][j] && (flag[i][j] < 2 || flag[i][j] > 3)) {
30+
board[i][j] = 0;
31+
} else if (board[i][j] && flag[i][j] >= 2 && flag[i][j] <= 3 || !board[i][j] && flag[i][j] === 3) {
32+
board[i][j] = 1;
33+
}
34+
}
35+
}
36+
};

0 commit comments

Comments
 (0)