794 Valid Tic-Tac-Toe State
A Tic-Tac-Toe board is given as a string arrayboard
. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
Theboard
is a 3 x 3 array, and consists of characters" "
,"X"
, and"O"
. The " " character represents an empty square.
Here are the rules of Tic-Tac-Toe:
Players take turns placing characters into empty squares (" ").
The first player always places "X" characters, while the second player always places "O" characters.
"X" and "O" characters are always placed into empty squares, never filled ones.
The game ends when there are 3 of the same (non-empty) character filling any row, column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Note:
board
is a length-3 array of strings, where each stringboard[i]
has length 3.Each
board[i][j]
is a character in the set{" ", "X", "O"}
.
这题不看答案真不知道怎么做。起码有5中case要handle。除了上述4种情况以外,还有一种就是:“XXX”,“OO_", "O "。这种情况应该返回false,因为X已经赢了,O不能再放了。这题需要用一个turn变量来check board上的X和O数目是否合法。他们之间只能差1,当我们见到X的时候turn++,O的时候turn--。如果turn最后不是0或1的话,证明某一个多放了。(1表示X多一个,0的时候数目相等)。另外我们还需要两个变量xwin和owin来记录是否有人赢了。如果有我们应该结束游戏,用来判断第5种情况。
Last updated