File tree 1 file changed +6
-5
lines changed
scripts/algorithms/G/Game of Life
1 file changed +6
-5
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 101 ms (Top 32.25%) | Memory: 42 MB (Top 79.17%)
1
2
/**
2
3
* @param {number[][] } board
3
4
* @return {void } Do not return anything, modify board in-place instead.
4
5
*/
5
6
var gameOfLife = function ( board ) {
6
7
const m = board . length , n = board [ 0 ] . length ;
7
8
let copy = JSON . parse ( JSON . stringify ( board ) ) ;
8
-
9
+
9
10
const getNeighbor = ( row , col ) => {
10
11
let radius = [ - 1 , 0 , 1 ] , count = 0 ;
11
12
for ( let i = 0 ; i < 3 ; i ++ ) {
@@ -16,17 +17,17 @@ var gameOfLife = function(board) {
16
17
count ++ ;
17
18
}
18
19
}
19
- }
20
+ }
20
21
}
21
22
return count ;
22
23
}
23
-
24
+
24
25
for ( let i = 0 ; i < m ; i ++ ) {
25
26
for ( let j = 0 ; j < n ; j ++ ) {
26
27
const count = getNeighbor ( i , j ) ;
27
28
if ( copy [ i ] [ j ] == 1 ) {
28
29
if ( count < 2 || count > 3 ) {
29
- board [ i ] [ j ] = 0 ;
30
+ board [ i ] [ j ] = 0 ;
30
31
}
31
32
} else {
32
33
if ( count == 3 ) {
@@ -35,4 +36,4 @@ var gameOfLife = function(board) {
35
36
}
36
37
}
37
38
}
38
- } ;
39
+ } ;
You can’t perform that action at this time.
0 commit comments