Naive Python solution

  • 0
    class TicTacToe(object):
        def __init__(self, n):
            Initialize your data structure here.
            :type n: int
            self.board = [[0] * n for _ in xrange(n)]
            self.n = n
        def move(self, row, col, player):
            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.
            :type row: int
            :type col: int
            :type player: int
            :rtype: int
            n = self.n
            self.board[row][col] = player
            if all(x == player for x in self.board[row]) \
                or all(x == player for x in zip(*self.board)[col]) \
                or (row == col and all(self.board[i][i] == player for i in xrange(n))) \
                or (row+col == n-1 and all(self.board[i][n-i-1] == player for i in xrange(n))):
                    return player
            else: return 0

Log in to reply

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