diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/1.jpg b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/1.jpg new file mode 100644 index 000000000..abe12ce81 Binary files /dev/null and b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/1.jpg differ diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/2.jpg b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/2.jpg new file mode 100644 index 000000000..b83663981 Binary files /dev/null and b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/2.jpg differ diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/3.jpg b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/3.jpg new file mode 100644 index 000000000..f166c34c0 Binary files /dev/null and b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/3.jpg differ diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md index eb117afb6..b7673940c 100644 --- a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md +++ b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md @@ -1,28 +1,48 @@ # [794.Valid Tic-Tac-Toe State][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description -**Example 1:** +Given a Tic-Tac-Toe `board` 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 that 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 three 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:** + +![1](./1.jpg) ``` -Input: a = "11", b = "1" -Output: "100" +Input: board = ["O "," "," "] +Output: false +Explanation: The first player always plays "X". ``` -## 题意 -> ... +**Example 2:** -## 题解 +![2](./2.jpg) -### 思路1 -> ... -Valid Tic-Tac-Toe State -```go ``` +Input: board = ["XOX"," X "," "] +Output: false +Explanation: Players take turns making moves. +``` + +**Example 3:** + +![3](./3.jpg) +``` +Input: board = ["XOX","O O","XOX"] +Output: true +``` ## 结语 diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution.go b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution.go index d115ccf5e..8bc07fedd 100644 --- a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution.go +++ b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution.go @@ -1,5 +1,66 @@ package Solution -func Solution(x bool) bool { - return x +func win794(board []string) (bool, bool) { + x, o := false, false + for i := range 3 { + if board[i][0] == board[i][1] && board[i][1] == board[i][2] { + if board[i][0] == 'X' { + x = true + } + if board[i][0] == 'O' { + o = true + } + } + + if board[0][i] == board[1][i] && board[1][i] == board[2][i] { + if board[0][i] == 'X' { + x = true + } + if board[0][i] == 'O' { + o = true + } + } + } + if board[0][0] == board[1][1] && board[1][1] == board[2][2] { + if board[0][0] == 'X' { + x = true + } + if board[0][0] == 'O' { + o = true + } + } + if board[0][2] == board[1][1] && board[1][1] == board[2][0] { + if board[0][2] == 'X' { + x = true + } + if board[0][2] == 'O' { + o = true + } + } + return x, o +} + +func Solution(board []string) bool { + xWin, oWin := win794(board) + if xWin && oWin { + return false + } + x, o := 0, 0 + for i := range 3 { + for j := range 3 { + if board[i][j] == 'X' { + x++ + } + if board[i][j] == 'O' { + o++ + } + } + } + if !xWin && !oWin { + return x == o || x == o+1 + } + if xWin { + return x == o+1 + } + return x == o } diff --git a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go index 14ff50eb4..63a0e9579 100644 --- a/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go +++ b/leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs []string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"O ", " ", " "}, false}, + {"TestCase2", []string{"XOX", " X ", " "}, false}, + {"TestCase3", []string{"XOX", "O O", "XOX"}, true}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }