Easy to understand Accepted Java Solution


  • 0
    R
    class TicTacToe {
    
        /** Initialize your data structure here. */
        
        private int n;
        private int[][] rows;
        private int[][] cols;
        private int[] d1;
        private int[] d2;
        
        public TicTacToe(int n) {
            this.n = n;
            rows = new int[n][2];
            cols = new int[n][2];
            d1 = new int[2];
            d2 = new int[2];
        }
        
        /** Player {player} makes a move at ({row}, {col}).
            @param row The row of the board.
            @param col The column of the board.
            @param player The player, can be either 1 or 2.
            @return The current winning condition, can be either:
                    0: No one wins.
                    1: Player 1 wins.
                    2: Player 2 wins. */
        public int move(int row, int col, int player) {
            if (player == 1) {
               rows[row][0]++;
               cols[col][0]++;
               if (row == col) {
                   d1[0]++;
               }
               if (col == n - row - 1) {
                   d2[0]++;
               }
               if (rows[row][0] == n || cols[col][0] == n || d1[0] == n || d2[0] == n) {
                   return 1;
               }
            } else {
                rows[row][1]++;
                cols[col][1]++;
                if (row == col) {
                   d1[1]++;
                }
                if (col == n - row - 1) {
                   d2[1]++;
                }
                 if (rows[row][1] == n || cols[col][1] == n || d1[1] == n || d2[1] == n) {
                   return 2;
               }
            }
            return 0;
        }
    }
    

Log in to reply
 

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