leetcode-289- jeu de la vie

 

 Solution a: O (MN) O (MN)) version Java

classe Solution {
     publique  vide gameOfLife ( int [] [] bord) {
         int [] voisins = {0,1, -1 };
        int rangs = board.length;
        int cols = planche [0 ] .length;
        int [] [] = Copyboard nouveau  int [lignes] [cols];
        pour ( int row = 0; <rangée des rangées; rangée ++ ) {
             pour ( int col = 0; col <cols, col ++ ) { 
                Copyboard [ligne] [col] = planche [ligne] [col]; 
            } 
        }
        pour ( int row = 0; <rangée des rangées; rangée ++ ) {
             pour ( int col = 0; <col cols, col ++ ) {
                 int liveNeighbors = 0 ;
                pour ( int i = 0; i <3; i ++ ) {
                     pour ( int j = 0; j <3; j ++ ) {
                         si ((voisins [i] == 0 && voisins [j] == 0! )) {
                             int r = (ligne + voisins [i]);
                            int c = (+ col voisins [j]);
                            si((r <&& rangées r> = 0) && (c <&& cols c> = 0) && (Copyboard [r] [c] == 1 )) { 
                                liveNeighbors + = 1 ; 
                            } 
                        } 
                    } 
                } 
                Si (([Copyboard ligne] [col] == 1) && (liveNeighbors <2 || liveNeighbors> 3 )) { 
                    planche [ligne] [col] = 0 ; 
                } 
                Si (Copyboard [ligne] [col] == 0 && liveNeighbors == 3 ) { 
                    planche [ligne] [col] = 1 ;

Deuxième méthode: O (mn) O (1)

classe Solution {
     publique  vide gameOfLife ( int [] [] bord) {
         int [] voisins = {0,1, -1 };
        int rangs = board.length;
        int cols = planche [0 ] .length; 
        
        pour ( int row = 0; <rangée des rangées; rangée ++ ) {
             pour ( int col = 0; <col cols, col ++ ) {
                 int liveNeighbors = 0 ;
                pour ( int i = 0; i <3; i ++ ) {
                     pour ( intj = 0; j <3; j ++ ) {
                         si ((voisins [i] == 0 && voisins [j] == 0! )) {
                             int r = (ligne + voisins [i]);
                            int c = (+ col voisins [j]);
                            si ((r <&& rangées r> = 0) && (c <&& cols c> = 0) && (Math.abs (planche [r] [c]) == 1 )) { 
                                liveNeighbors + = 1 ; 
                            } 
                        } 
                    } 
                } 
                Si ((planche [ligne] [col] == 1) && (liveNeighbors <2 || liveNeighbors> 3
                    conseil [ligne] [col] = -1 ; 
                } 
                Si (planche [ligne] [col] == 0 && liveNeighbors == 3 ) { 
                    planche [ligne] [col] = 2 ; 
                } 
            } 
        } 
        
        Pour ( int row = 0; rangée <rangées; rangée ++ ) {
             pour ( int col = 0; col <cols, col ++ ) {
                 si (planche [ligne] [col]> 0 ) { 
                    planche [ligne] [col ] = 1 ; 
                } Autre { 
                    conseil [suite] [col] = 0; 
                } 

            } 
        } 
    } 
}

 

Je suppose que tu aimes

Origine www.cnblogs.com/oldby/p/12620843.html
conseillé
Classement