Clear this solution by using js


  • 1
    D
    var gameOfLife = function(board) {
            var row = board.length;
            var col = board[0].length;
            for(var x1=0;x1<row;x1++){
              for(var x2=0;x2<col;x2++){
                  var tag=0;
                  if(board[x1][x2]===0){tag=1}
                  for(var i=-1;i<2;i++){
                      for(var n=-1;n<2;n++){
                          if(x1+i>=0&&x1+i<row&&x2+n>=0&&x2+n<col){
                              if((i!==0||n!==0)&&board[x1+i][x2+n]>0){board[x1][x2]++;}
                          }
                      }
                  }
                  if(tag===1){board[x1][x2] = -board[x1][x2];}
              }  
            }
            for(x1=0;x1<row;x1++){
              for(x2=0;x2<col;x2++){   
                 if(board[x1][x2]===-3){board[x1][x2]=1}
                 else if(board[x1][x2]>2&&board[x1][x2]<5){board[x1][x2]=1}
                 else{board[x1][x2]=0}
              }
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.