1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
valid-tic-tac-toe-state.py 946 Bytes
一键复制 编辑 原始数据 按行查看 历史
# Time: O(1)
# Space: O(1)
class Solution(object):
def validTicTacToe(self, board):
"""
:type board: List[str]
:rtype: bool
"""
def win(board, player):
for i in xrange(3):
if all(board[i][j] == player for j in xrange(3)):
return True
if all(board[j][i] == player for j in xrange(3)):
return True
return (player == board[1][1] == board[0][0] == board[2][2] or \
player == board[1][1] == board[0][2] == board[2][0])
FIRST, SECOND = ('X', 'O')
x_count = sum(row.count(FIRST) for row in board)
o_count = sum(row.count(SECOND) for row in board)
if o_count not in {x_count-1, x_count}: return False
if win(board, FIRST) and x_count-1 != o_count: return False
if win(board, SECOND) and x_count != o_count: return False
return True
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助