348. Design Tic-Tac-Toe

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Design a Tic-tac-toe game that is played between two play...
    我是你的果果呀阅读 2,934评论 0 0
  • Design a Tic-tac-toe game that is played between two play...
    Jeanz阅读 3,952评论 0 0
  • https://leetcode.com/problems/design-tic-tac-toe/discuss/...
    matrxyz阅读 1,387评论 0 0
  • 透过时间的缝隙 过往仿佛变成了纸片 一页页翻过 我就像一名游客 穿梭于我曾经经历的大街小巷 像看他人的故事看着我...
    miss熊小熊阅读 1,076评论 0 0
  • 无论你昨晚多么的泣不成声 今天的日子还得照样过 太阳照常升起 世间万物都有自己的行走轨迹
    圆逗逗阅读 2,707评论 2 5

友情链接更多精彩内容