Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 33 additions & 13 deletions leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
65 changes: 63 additions & 2 deletions leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 6 additions & 6 deletions leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading