其他分享
首页 > 其他分享> > leetcode-289-生命游戏

leetcode-289-生命游戏

作者:互联网

 

 解法一:O(MN)O(MN))Java版

class Solution {
    public void gameOfLife(int[][] board) {
        int [] neighbors = {0,1,-1};
        int rows = board.length;
        int cols = board[0].length;
        int [][] copyBoard = new int [rows][cols];
        for(int row = 0;row<rows;row++){
            for(int col = 0;col< cols; col++){
                copyBoard[row][col] = board[row][col];
            }
        }
        for(int row = 0; row <rows;row++){
            for(int col = 0; col < cols;col++){
                int liveNeighbors = 0;
                for(int i =0; i <3;i++){
                    for(int j = 0; j < 3;j++){
                        if(!(neighbors[i] == 0 && neighbors[j] == 0)){
                            int r = (row +neighbors[i]);
                            int c = (col + neighbors[j]);
                            if ((r <rows && r>=0)&&(c < cols && c>= 0)&&(copyBoard[r][c] == 1)){
                                liveNeighbors += 1;
                            }
                        }
                    }
                }
                if((copyBoard[row][col] == 1)&&(liveNeighbors <2 || liveNeighbors >3)){
                    board[row][col] = 0;
                }
                if (copyBoard[row][col] == 0&& liveNeighbors == 3){
                    board[row][col] = 1;
                }
            }
        }
    }
}

方法二:O(mn)O(1)

class Solution {
    public void gameOfLife(int[][] board) {
        int [] neighbors = {0,1,-1};
        int rows = board.length;
        int cols = board[0].length;
        
        for(int row = 0; row <rows;row++){
            for(int col = 0; col < cols;col++){
                int liveNeighbors = 0;
                for(int i =0; i <3;i++){
                    for(int j = 0; j < 3;j++){
                        if(!(neighbors[i] == 0 && neighbors[j] == 0)){
                            int r = (row +neighbors[i]);
                            int c = (col + neighbors[j]);
                            if ((r <rows && r>=0)&&(c < cols && c>= 0)&&(Math.abs(board[r][c]) == 1)){
                                liveNeighbors += 1;
                            }
                        }
                    }
                }
                if((board[row][col] == 1)&&(liveNeighbors <2 || liveNeighbors >3)){
                    board[row][col] = -1;
                }
                if (board[row][col] == 0&& liveNeighbors == 3){
                    board[row][col] = 2;
                }
            }
        }
        
        for(int row = 0;row<rows;row++){
            for(int col = 0;col< cols; col++){
                if (board[row][col] > 0){
                    board[row][col] = 1;
                }else{
                    board[row][col] = 0;
                }

            }
        }
    }
}

 

标签:游戏,int,col,289,board,&&,leetcode,liveNeighbors,row
来源: https://www.cnblogs.com/oldby/p/12620843.html