美文网首页
348. Design Tic-Tac-Toe

348. Design Tic-Tac-Toe

作者: Super_Alan | 来源:发表于2018-05-02 12:29 被阅读0次

LeetCode Link

size 为 n 的棋牌,player1 和 player2 轮流向空处置子,当 row, column or diagonal 有 n 个相同的子,游戏结束,返回胜利的player,否则返回 0.

initial implementation

class TicTacToe {
    int[][] board;
    int size;
    
    public TicTacToe(int n) {
        size = n;
        board = new int[n][n];
    }
    
    // if the player wins, return the player; otherwise, return 0
    public int move(int row, int col, int player) {
        board[row][col] = player;
        
        // row
        int playerCnt = 0;
        for (int i = 0; i < size; i++) {
            if (board[row][i] == player) {
                playerCnt++;
            } else {
                break;
            }
        }
        
        if (playerCnt == size) {
            return player;
        }
        
        // col
        playerCnt = 0;
        for (int i = 0; i < size; i++) {
            if (board[i][col] == player) {
                playerCnt++;
            } else {
                break;
            }
        }
        
        if (playerCnt == size) {
            return player;
        }
        
        // left top to right bottom diagonal
        playerCnt = 0;
        if (row == col) {
            for (int i = 0; i < size; i++) {
                if (board[i][i] == player) {
                    playerCnt++;
                } else {
                    break;
                }
            }
        }
        
        if (playerCnt == size) {
            return player;
        }
        
        // left bottom to right top diagonal
        playerCnt = 0;
        if (row + col == size - 1) {
            for (int i = 0; i < size; i++) {
                if (board[i][size - 1 - i] == player) {
                    playerCnt++;
                } else {
                    break;
                }
            }
        }
        
        if (playerCnt == size) {
            return player;
        }
        
        return 0;
    }
}

这个题目本身假设每一次 move 都是有效的,也就是说,并不需要记录 board 具体每个 cell 的值。因此可以优化成下面的代码:

class TicTacToe {
    int size;
    int[][] rowCount;
    int[][] colCount;
    int[] topToBottom;
    int[] bottomToTop;
    
    public TicTacToe(int n) {
        size = n;
        // rowCount[playerIndex][row]: token count of player (playerIndex + 1) at row 'row' of the board
        rowCount = new int[2][n];
        
        // colCount[playerIndex][col]: token count of player (playerIndex + 1) at column 'col' of the board
        colCount = new int[2][n];
        
        // topToBottom[playerIndex]: token count of player (playerIndex + 1) on the diagonal top to bottom
        topToBottom = new int[2];
        
        // bottomToTop[playerIndex]: token count of player (playerIndex + 1) on the diagonal bottom to top
        bottomToTop = new int[2];
    }
    
    // if the player wins, return the player; otherwise, return 0
    public int move(int row, int col, int player) {
        int playerIndex = player - 1;
        
        rowCount[playerIndex][row] += 1;
        colCount[playerIndex][col] += 1;
        if (row == col) {
            topToBottom[playerIndex] += 1;
        }
        if (row + col == size - 1) {
            bottomToTop[playerIndex] += 1;
        }
        
        if (rowCount[playerIndex][row] == size || colCount[playerIndex][col] == size 
                || topToBottom[playerIndex] == size || bottomToTop[playerIndex] == size) {
            return player;
        }
        
        return 0;
    }
}

Runtime: O(n) -> O(1)
Space: O(n ^ 2) -> O(n)

上面题解的二维数组还可以进一步优化为一维数组。

int step = (player == 1) ? 1: -1;

rows[row] += step;
cols[col] += step;
diagonal += step if row == col
antiDiagonal += step if row + col == size - 1

return player if (any == size || any == -size)

相关文章

  • 348. Design Tic-Tac-Toe

    LeetCode Link size 为 n 的棋牌,player1 和 player2 轮流向空处置子,当 ro...

  • 348. Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two play...

  • 348. Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two play...

  • 348. Design Tic-Tac-Toe

    https://leetcode.com/problems/design-tic-tac-toe/discuss/...

  • Design Tic-Tac-Toe

    题目Design a Tic-tac-toe game that is played between two pl...

  • Ilya and tic-tac-toe game

    Ilya and tic-tac-toe game Ilya is an experienced player i...

  • Ilya and tic-tac-toe game

    Ilya is an experienced player in tic-tac-toe on the4 × 4f...

  • LeetCode 1275. Find Winner on a

    Tic-tac-toe is played by two players A and B on a 3 x 3 g...

  • 348.家风

    “王华到翰林院工作后,偶尔被调到冬宫给太子朱厚照讲课,太监刘瑾在那时和王华有旧。刘瑾还曾多次向王华请教一些知识,王...

  • 348. 秋实

    秋雨把绿叶的身姿卷在行人脚下,沾满泥水葱郁旺盛的绿色在一场秋雨一场寒中拜倒在秋天的石榴裙下 不管是秋雨,还是秋风扫...

网友评论

      本文标题:348. Design Tic-Tac-Toe

      本文链接:https://www.haomeiwen.com/subject/hiaqrftx.html