LeetCode 794. [Python] Valid Tic-Tac-Toe State
原题链接
中等
作者:
徐辰潇
,
2020-04-02 00:04:20
,
所有人可见
,
阅读 859
class Solution:
def validTicTacToe(self, board: List[str]) -> bool:
NumO = 0
NumX = 0
for i in range(3):
for j in range(3):
NumO += (board[i][j] == 'O')
NumX += (board[i][j] == 'X')
if NumX < NumO or NumX > NumO + 1:
return False
Dir = [[0,1], [1, 0], [1, 1], [-1,1]]
def helper(x0, y0, Player):
for dir in Dir:
x = x0
y = y0
for step in range(2):
x += dir[0]
y += dir[1]
if (0 <= x < 3 and 0 <= y < 3 and board[x][y] == Player):
if step == 1:
return True
else:
break
return False
BoolX = False
BoolO = False
for i in range(3):
for j in range(3):
if board[i][j] == 'X':
BoolX = BoolX or helper(i, j, 'X')
if board[i][j] == 'O':
BoolO = BoolO or helper(i, j, 'O')
if BoolX and BoolO:
return False
if BoolX:
if NumX == NumO:
return False
if BoolO:
if NumX > NumO:
return False
return True