Skip to content

Commit ea6b043

Browse files
committed
Add solution and test-cases for problem 794
1 parent f03226b commit ea6b043

File tree

6 files changed

+102
-21
lines changed

6 files changed

+102
-21
lines changed
5.19 KB
Loading
7.43 KB
Loading
10.5 KB
Loading

leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/README.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
11
# [794.Valid Tic-Tac-Toe State][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

8-
**Example 1:**
5+
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.
6+
7+
The board is a `3 x 3` array that consists of characters `' '`, `'X'`, and `'O'`. The `' '` character represents an empty square.
8+
9+
Here are the rules of Tic-Tac-Toe:
10+
11+
- Players take turns placing characters into empty squares `' '`.
12+
- The first player always places `'X'` characters, while the second player always places `'O'` characters.
13+
- `'X'` and `'O'` characters are always placed into empty squares, never filled ones.
14+
- The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
15+
- The game also ends if all squares are non-empty.
16+
- No more moves can be played if the game is over.
17+
18+
**Example 1:**
19+
20+
![1](./1.jpg)
921

1022
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
23+
Input: board = ["O "," "," "]
24+
Output: false
25+
Explanation: The first player always plays "X".
1326
```
1427

15-
## 题意
16-
> ...
28+
**Example 2:**
1729

18-
## 题解
30+
![2](./2.jpg)
1931

20-
### 思路1
21-
> ...
22-
Valid Tic-Tac-Toe State
23-
```go
2432
```
33+
Input: board = ["XOX"," X "," "]
34+
Output: false
35+
Explanation: Players take turns making moves.
36+
```
37+
38+
**Example 3:**
39+
40+
![3](./3.jpg)
2541

42+
```
43+
Input: board = ["XOX","O O","XOX"]
44+
Output: true
45+
```
2646

2747
## 结语
2848

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,66 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func win794(board []string) (bool, bool) {
4+
x, o := false, false
5+
for i := range 3 {
6+
if board[i][0] == board[i][1] && board[i][1] == board[i][2] {
7+
if board[i][0] == 'X' {
8+
x = true
9+
}
10+
if board[i][0] == 'O' {
11+
o = true
12+
}
13+
}
14+
15+
if board[0][i] == board[1][i] && board[1][i] == board[2][i] {
16+
if board[0][i] == 'X' {
17+
x = true
18+
}
19+
if board[0][i] == 'O' {
20+
o = true
21+
}
22+
}
23+
}
24+
if board[0][0] == board[1][1] && board[1][1] == board[2][2] {
25+
if board[0][0] == 'X' {
26+
x = true
27+
}
28+
if board[0][0] == 'O' {
29+
o = true
30+
}
31+
}
32+
if board[0][2] == board[1][1] && board[1][1] == board[2][0] {
33+
if board[0][2] == 'X' {
34+
x = true
35+
}
36+
if board[0][2] == 'O' {
37+
o = true
38+
}
39+
}
40+
return x, o
41+
}
42+
43+
func Solution(board []string) bool {
44+
xWin, oWin := win794(board)
45+
if xWin && oWin {
46+
return false
47+
}
48+
x, o := 0, 0
49+
for i := range 3 {
50+
for j := range 3 {
51+
if board[i][j] == 'X' {
52+
x++
53+
}
54+
if board[i][j] == 'O' {
55+
o++
56+
}
57+
}
58+
}
59+
if !xWin && !oWin {
60+
return x == o || x == o+1
61+
}
62+
if xWin {
63+
return x == o+1
64+
}
65+
return x == o
566
}

leetcode/701-800/0794.Valid-Tic-Tac-Toe-State/Solution_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"O ", " ", " "}, false},
17+
{"TestCase2", []string{"XOX", " X ", " "}, false},
18+
{"TestCase3", []string{"XOX", "O O", "XOX"}, true},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)