Nothing special, but easy to understand code.


  • 0
    T

    Just share my solution. I believe my code is simple and easy to understand.

    public class Solution {
        public void gameOfLife(int[][] board) {
            Set<Point> updated = new HashSet<>();
            for(int y = 0; y < board.length; y++){
                for(int x = 0; x < board[0].length; x++){
                    int livingNeighbour = getLivingNeighbour(y, x, board, updated);
                    if(livingNeighbour < 2){
                        if(board[y][x] == 1){
                            updated.add(Point.create(y, x));
                        }
                        board[y][x] = 0;
                    }else if(livingNeighbour == 2){
                        //Nothing will happen..
                        board[y][x] = board[y][x];
                    }else if(livingNeighbour == 3){
                        if(board[y][x] == 0){
                            updated.add(Point.create(y, x));
                        }
                        board[y][x] = 1; // reborn
                    }else {
                        if(board[y][x] == 1){
                            updated.add(Point.create(y, x));
                        }
                        board[y][x] = 0; // over population
                    }
                }
            }
        }
    
        private int getLivingNeighbour(int y, int x, int[][] board, Set<Point> updated) {
            int[] nys = getNeighbourCoor(y, board.length);
            int[] nxs = getNeighbourCoor(x, board[0].length);
            int lived = 0;
            for(int ny : nys){
                for(int nx : nxs){
                    if(ny == y && nx == x){
                        continue;
                    }
                    if((!updated.contains(Point.create(ny, nx)) && board[ny][nx] == 1) ||
                            (updated.contains(Point.create(ny, nx)) && board[ny][nx] == 0)){
                        lived++;
                    }
                }
            }
            return lived;
        }
    
        private int[] getNeighbourCoor(int coor, int n){
            int prev = coor - 1;
            int next = coor + 1;
            List<Integer> coorList = new LinkedList<>();
            coorList.add(coor);
            if(prev != -1){
                coorList.add(prev);
            }
            if(next !=  n){
                coorList.add(next);
            }
            return coorList.stream().mapToInt(i->i).toArray();
        }
    
        private static class Point{
            final int x;
            final int y;
    
            static Point create(int y, int x){
                return new Point(y, x);
            }
            private Point(int y, int x){
                this.x = x;
                this.y = y;
            }
    
            @Override
            public boolean equals(Object o){
                if(o instanceof Point){
                    Point other = (Point)o;
                    if(other.x == x && other.y == y){
                        return true;
                    }
                }
                return false;
            }
    
            @Override
            public int hashCode() {
                return Objects.hash(x, y);
            }
        }
    }
    

Log in to reply
 

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