Simple C++ Solution


  • 0
    G
        struct Player {
            array<int*, 2> rc;  // counters for rows and columns
            array<int, 2> d;    // counters for two diagonals
        };
        int _n;
        Player p[2];
    
        /** Initialize your data structure here. */
        TicTacToe(int n) : _n(n) {
            // Initializing counters to 0
            for (int i = 0; i < 2; i++) {
                p[i].rc[0] = new int[n];
                p[i].rc[1] = new int[n];
                for (int j = 0; j < n; j++) {
                    p[i].rc[0][j] = p[i].rc[1][j] = 0;
                }
                p[i].d[0] = p[i].d[1] = 0;
            }
        }
    
        int move(int row, int col, int player) {
            if (++p[player - 1].rc[0][row] == _n) return player;
            if (++p[player - 1].rc[1][col] == _n) return player;
            if (row == col && ++p[player - 1].d[0] == _n) return player;
            if (row + col + 1 == _n && ++p[player - 1].d[1] == _n) return player;
            return 0;
        }
    

Log in to reply
 

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