Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
design-tic-tac-toe.py 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 13:15 +08:00 . remove unused code
# Time: O(1), per move.
# Space: O(n^2)
class TicTacToe(object):
def __init__(self, n):
"""
Initialize your data structure here.
:type n: int
"""
self.__size = n
self.__rows = [[0, 0] for _ in xrange(n)]
self.__cols = [[0, 0] for _ in xrange(n)]
self.__diagonal = [0, 0]
self.__anti_diagonal = [0, 0]
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
"""
i = player - 1
self.__rows[row][i] += 1
self.__cols[col][i] += 1
if row == col:
self.__diagonal[i] += 1
if col == len(self.__rows) - row - 1:
self.__anti_diagonal[i] += 1
if any(self.__rows[row][i] == self.__size,
self.__cols[col][i] == self.__size,
self.__diagonal[i] == self.__size,
self.__anti_diagonal[i] == self.__size):
return player
return 0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助