[Swift]LeetCode794. 有效的井字游戏 | Valid Tic-Tac-Toe State
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10547120.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
A Tic-Tac-Toe board is given as a string array board
. Return True if and only if it is possible to reach this board position during the course of a valid tic-tac-toe game.
The board
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.
Example 1: Input: board = ["O ", " ", " "] Output: false Explanation: The first player always plays "X". Example 2: Input: board = ["XOX", " X ", " "] Output: false Explanation: Players take turns making moves. Example 3: Input: board = ["XXX", " ", "OOO"] Output: false Example 4: Input: board = ["XOX", "O O", "XOX"] Output: true
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"}
.
用字符串数组作为井字游戏的游戏板 board
。当且仅当在井字游戏过程中,玩家有可能将字符放置成游戏板所显示的状态时,才返回 true。
该游戏板是一个 3 x 3 数组,由字符 " "
,"X"
和 "O"
组成。字符 " "
代表一个空位。
以下是井字游戏的规则:
- 玩家轮流将字符放入空位(" ")中。
- 第一个玩家总是放字符 “X”,且第二个玩家总是放字符 “O”。
- “X” 和 “O” 只允许放置在空位中,不允许对已放有字符的位置进行填充。
- 当有 3 个相同(且非空)的字符填充任何行、列或对角线时,游戏结束。
- 当所有位置非空时,也算为游戏结束。
- 如果游戏结束,玩家不允许再放置字符。
示例 1: 输入: board = ["O ", " ", " "] 输出: false 解释: 第一个玩家总是放置“X”。 示例 2: 输入: board = ["XOX", " X ", " "] 输出: false 解释: 玩家应该是轮流放置的。 示例 3: 输入: board = ["XXX", " ", "OOO"] 输出: false 示例 4: 输入: board = ["XOX", "O O", "XOX"] 输出: true
说明:
- 游戏板
board
是长度为 3 的字符串数组,其中每个字符串board[i]
的长度为 3。 -
board[i][j]
是集合{" ", "X", "O"}
中的一个字符。
1 class Solution { 2 func validTicTacToe(_ board: [String]) -> Bool { 3 var xwin:Bool = false 4 var owin:Bool = false 5 var row:[Int] = [Int](repeating:0,count:3) 6 var col:[Int] = [Int](repeating:0,count:3) 7 var diag:Int = 0 8 var antidiag:Int = 0 9 var turns:Int = 0 10 for i in 0..<3 11 { 12 for j in 0..<3 13 { 14 if board[i][j] == "X" 15 { 16 row[i] += 1 17 col[j] += 1 18 turns += 1 19 if i == j {diag += 1} 20 if i + j == 2 {antidiag += 1} 21 } 22 else if board[i][j] == "O" 23 { 24 row[i] -= 1 25 col[j] -= 1 26 turns -= 1 27 if i == j {diag -= 1} 28 if i + j == 2 {antidiag -= 1} 29 } 30 } 31 } 32 xwin = row[0] == 3 || row[1] == 3 || row[2] == 3 || 33 col[0] == 3 || col[1] == 3 || col[2] == 3 || 34 diag == 3 || antidiag == 3 35 owin = row[0] == -3 || row[1] == -3 || row[2] == -3 || 36 col[0] == -3 || col[1] == -3 || col[2] == -3 || 37 diag == -3 || antidiag == -3 38 if (xwin && turns == 0) || (owin && turns == 1) {return false} 39 return (turns == 0 || turns == 1) && (!xwin || !owin) 40 } 41 } 42 43 //String扩展 44 extension String { 45 //subscript函数可以检索数组中的值 46 //直接按照索引方式截取指定索引的字符 47 subscript (_ i: Int) -> Character { 48 //读取字符 49 get {return self[index(startIndex, offsetBy: i)]} 50 } 51 }
8ms
1 class Solution { 2 3 func validTicTacToe(_ board: [String]) -> Bool { 4 var xW = 0 5 var oW = 0 6 var numX = 0 7 var numO = 0 8 9 for i in 0..<board.count { 10 for j in 0..<board[i].count { 11 checkSet(board[i][j], &numX, &numO) 12 } 13 if board[0][i]==board[1][i] && board[1][i]==board[2][i]{ 14 checkSet(board[0][i], &xW, &oW) 15 } 16 17 if board[i][0]==board[i][1] && board[i][1]==board[i][2]{ 18 checkSet(board[i][0], &xW, &oW) 19 } 20 } 21 22 if board[0][0]==board[1][1] && board[1][1]==board[2][2]{ 23 checkSet(board[1][1], &xW, &oW); 24 } 25 26 if board[0][2]==board[1][1] && board[1][1]==board[2][0]{ 27 checkSet(board[1][1], &xW, &oW) 28 } 29 return (numX==numO && xW==0) || (numX == numO+1 && oW==0) 30 } 31 32 func checkSet(_ v: Character, _ x: inout Int, _ o:inout Int) { 33 if v == "X"{ 34 x += 1 35 } 36 else if v=="O" { 37 o += 1 38 } 39 } 40 } 41 42 extension String{ 43 subscript(index:Int)->Character{ 44 return self[self.index(self.startIndex, offsetBy:index)] 45 } 46 }