[LC] 348. Design Tic-Tac-Toe

这一题,我对它的follow up 其实感到略微好奇,因为我其实不知道O(n^2)是怎么做的,我只知道O(n)乃至O(1)的做法。

O(n) 做法如下。

第一种比较直观,需要O(n^2)的空间,用于真实装一个棋盘的数据。每次更新一个位置之后,就根据这个位置所在的位置,去检查更新位置对应的行,列,以及可能的两条对角线:col == row的左下到右上的对角或者col + row == n - 1的左上到右下的对角。每个检查都是O(n),然后总共最多四个O(n),所以总的还是O(n)。根据上述思想,得到代码如下:

    int[][] board;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        board = new int[n][n];
    }
    
    /** 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) {
        board[row][col] = player;
        boolean validate = lineCheck(true, col, player) || lineCheck(false, row, player);
        if (row == col) {
            validate |= diagonalCheck(false, player);
        }
        
        if (row == board.length - col - 1) {
            validate |= diagonalCheck(true, player);
        }
        
        return validate ? player : 0;
    }
    
    public boolean lineCheck(boolean isCol, int num, int player) {
        for (int i = 0; i < board.length; i++) {
            int val = isCol ? board[i][num] : board[num][i];
            if (val != player) {
                return false;
            }
        }
        
        return true;
    }
    
    public boolean diagonalCheck(boolean isReverse, int player) {
        for (int i = 0; i < board.length; i++) {
            int y = isReverse ? board.length - 1 - i : i;
            if (board[y][i] != player) {
                return false;
            }
        }
        
        return true;
    }

这题的做法很直观的,move()是O(n)复杂度,空间复杂度O(n^2)。

O(1)的做法思维还是有点意思的,它不需要记录全局的棋盘是如何的,只需要有两个数组分别记录行和列的状况,另外两个单独的integer记录两个对角线就可以了。对于任意一条行或者列,或者对角线,只要遇到player 1,就加一,遇到player 2,就减一,哪一个值先到了n或者-n,就表示哪一边胜利了。这种做法,是不需要一个n^2的数组去记录全局的,当然,这也是归功于题干给出的第一个前提,就是每一步都肯定是有效的,所以你不需要一个全局的二维数组去做validation。根据上面这个算法,可以得到代码如下:

    int[] rows, cols;
    int diagonal, antiDiag, n;
    
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        this.diagonal = this.antiDiag = 0;
        this.n = n;
        this.rows = new int[n];
        this.cols = new int[n];
    }
    
    /** 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) {
        int factor = player == 1 ? 1 : -1;
        this.rows[row] += factor;
        this.cols[col] += factor;
        if (row == col) {
            this.diagonal += factor;
        } 
        
        if (row + col == this.n - 1) {
            this.antiDiag += factor;
        }
        
        int winNum = this.n * factor;
        boolean win = this.rows[row] == winNum
            || this.cols[col] == winNum
            || this.diagonal == winNum
            || this.antiDiag == winNum;
        return win ? player : 0;
    }

你可以看到整个move,一个for循环都没有,多么明显的O(1)。就只需要做横,竖,两个对角线的一次计算,以及胜负判断,即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值