Skip to content

Commit 7930b8d

Browse files
committed
Update 794 solution
1 parent f6f2eeb commit 7930b8d

15 files changed

+257
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [794. Valid Tic-Tac-Toe State](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/)
1+
# [794. Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/)
22

33
## 题目
44

@@ -71,7 +71,7 @@ Here are the rules of Tic-Tac-Toe:
7171
分类模拟:
7272
- 根据题意棋盘在任意时候,要么 X 的数量比 O 的数量多 1,要么两者相等
7373
- X 的数量等于 O 的数量时,任何行、列或对角线都不会出现 3 个相同的 X
74-
- X 的数量比 O 的数量多 1时,任何行、列或对角线都不会出现 3 个相同的 O
74+
- X 的数量比 O 的数量多 1 时,任何行、列或对角线都不会出现 3 个相同的 O
7575

7676
## 代码
7777

leetcode/1034.Coloring-A-Border/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [1034. Coloring A Border](https://leetcode-cn.com/problems/coloring-a-border/)
1+
# [1034. Coloring A Border](https://leetcode.com/problems/coloring-a-border/)
22

33
## 题目
44

@@ -50,7 +50,7 @@ Return the final grid.
5050

5151
## 解题思路
5252

53-
- 用bfs进行遍历选出边界,使用color给边界着色
53+
- 用 bfs 进行遍历选出边界,使用 color 给边界着色
5454

5555
## 代码
5656

website/content/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,5 @@ func preimageSizeFZF1(K int) int {
104104
----------------------------------------------
105105
<div style="display: flex;justify-content: space-between;align-items: center;">
106106
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">⬅️上一页</a></p>
107-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
107+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State/">下一页➡️</a></p>
108108
</div>
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# [794. Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/)
2+
3+
## 题目
4+
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+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg)
21+
22+
Input: board = ["O "," "," "]
23+
Output: false
24+
Explanation: The first player always plays "X".
25+
26+
**Example 2**:
27+
28+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg)
29+
30+
Input: board = ["XOX"," X "," "]
31+
Output: false
32+
Explanation: Players take turns making moves.
33+
34+
**Example 3**:
35+
36+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg)
37+
38+
Input: board = ["XXX"," ","OOO"]
39+
Output: false
40+
41+
**Example 4**:
42+
43+
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg)
44+
45+
Input: board = ["XOX","O O","XOX"]
46+
Output: true
47+
48+
**Constraints:**
49+
50+
- board.length == 3
51+
- board[i].length == 3
52+
- board[i][j] is either 'X', 'O', or ' '.
53+
54+
## 题目大意
55+
56+
给你一个字符串数组 board 表示井字游戏的棋盘。当且仅当在井字游戏过程中,棋盘有可能达到 board 所显示的状态时,才返回 true 。
57+
58+
井字游戏的棋盘是一个 3 x 3 数组,由字符 ' ','X' 和 'O' 组成。字符 ' ' 代表一个空位。
59+
60+
以下是井字游戏的规则:
61+
62+
- 玩家轮流将字符放入空位(' ')中。
63+
- 玩家 1 总是放字符 'X' ,而玩家 2 总是放字符 'O' 。
64+
- 'X' 和 'O' 只允许放置在空位中,不允许对已放有字符的位置进行填充。
65+
- 当有 3 个相同(且非空)的字符填充任何行、列或对角线时,游戏结束。
66+
- 当所有位置非空时,也算为游戏结束。
67+
- 如果游戏结束,玩家不允许再放置字符。
68+
69+
## 解题思路
70+
71+
分类模拟:
72+
- 根据题意棋盘在任意时候,要么 X 的数量比 O 的数量多 1,要么两者相等
73+
- X 的数量等于 O 的数量时,任何行、列或对角线都不会出现 3 个相同的 X
74+
- X 的数量比 O 的数量多 1 时,任何行、列或对角线都不会出现 3 个相同的 O
75+
76+
## 代码
77+
78+
```go
79+
package leetcode
80+
81+
func validTicTacToe(board []string) bool {
82+
cntX, cntO := 0, 0
83+
for i := range board {
84+
for j := range board[i] {
85+
if board[i][j] == 'X' {
86+
cntX++
87+
} else if board[i][j] == 'O' {
88+
cntO++
89+
}
90+
}
91+
}
92+
if cntX < cntO || cntX > cntO+1 {
93+
return false
94+
}
95+
if cntX == cntO {
96+
return process(board, 'X')
97+
}
98+
return process(board, 'O')
99+
}
100+
101+
func process(board []string, c byte) bool {
102+
//某一行是"ccc"
103+
if board[0] == string([]byte{c, c, c}) || board[1] == string([]byte{c, c, c}) || board[2] == string([]byte{c, c, c}) {
104+
return false
105+
}
106+
//某一列是"ccc"
107+
if (board[0][0] == c && board[1][0] == c && board[2][0] == c) ||
108+
(board[0][1] == c && board[1][1] == c && board[2][1] == c) ||
109+
(board[0][2] == c && board[1][2] == c && board[2][2] == c) {
110+
return false
111+
}
112+
//某一对角线是"ccc"
113+
if (board[0][0] == c && board[1][1] == c && board[2][2] == c) ||
114+
(board[0][2] == c && board[1][1] == c && board[2][0] == c) {
115+
return false
116+
}
117+
return true
118+
}
119+
```
120+
121+
122+
----------------------------------------------
123+
<div style="display: flex;justify-content: space-between;align-items: center;">
124+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
125+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
126+
</div>

website/content/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ func getAnswerPerBound(nums []int, bound int) int {
5656

5757
----------------------------------------------
5858
<div style="display: flex;justify-content: space-between;align-items: center;">
59-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
59+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State/">⬅️上一页</a></p>
6060
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States/">下一页➡️</a></p>
6161
</div>

website/content/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
8787
----------------------------------------------
8888
<div style="display: flex;justify-content: space-between;align-items: center;">
8989
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1028.Recover-a-Tree-From-Preorder-Traversal/">⬅️上一页</a></p>
90-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1037.Valid-Boomerang/">下一页➡️</a></p>
90+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1034.Coloring-A-Border/">下一页➡️</a></p>
9191
</div>
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# [1034. Coloring A Border](https://leetcode.com/problems/coloring-a-border/)
2+
3+
## 题目
4+
5+
You are given an m x n integer matrix grid, and three integers row, col, and color. Each value in the grid represents the color of the grid square at that location.
6+
7+
Two squares belong to the same connected component if they have the same color and are next to each other in any of the 4 directions.
8+
9+
The border of a connected component is all the squares in the connected component that are either 4-directionally adjacent to a square not in the component, or on the boundary of the grid (the first or last row or column).
10+
11+
You should color the border of the connected component that contains the square grid[row][col] with color.
12+
13+
Return the final grid.
14+
15+
**Example 1**:
16+
17+
Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
18+
Output: [[3,3],[3,2]]
19+
20+
**Example 2**:
21+
22+
Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
23+
Output: [[1,3,3],[2,3,3]]
24+
25+
**Example 3**:
26+
27+
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
28+
Output: [[2,2,2],[2,1,2],[2,2,2]]
29+
30+
**Constraints:**
31+
32+
- m == grid.length
33+
- n == grid[i].length
34+
- 1 <= m, n <= 50
35+
- 1 <= grid[i][j], color <= 1000
36+
- 0 <= row < m
37+
- 0 <= col < n
38+
39+
## 题目大意
40+
41+
给你一个大小为 m x n 的整数矩阵 grid ,表示一个网格。另给你三个整数 row、col 和 color 。网格中的每个值表示该位置处的网格块的颜色。
42+
43+
当两个网格块的颜色相同,而且在四个方向中任意一个方向上相邻时,它们属于同一连通分量
44+
45+
边界:在连通分量的块中(前提)并且满足以下条件之一:
46+
(1)要么上下左右存在一个块不在连通分量里面
47+
(2)要么这个块的位置在整个grid的边框上
48+
49+
请你使用指定颜色 color 为所有包含网格块 grid[row][col] 的连通分量的边界进行着色,并返回最终的网格 grid 。
50+
51+
## 解题思路
52+
53+
- 用 bfs 进行遍历选出边界,使用 color 给边界着色
54+
55+
## 代码
56+
57+
```go
58+
package leetcode
59+
60+
type point struct {
61+
x int
62+
y int
63+
}
64+
65+
type gridInfo struct {
66+
m int
67+
n int
68+
grid [][]int
69+
originalColor int
70+
}
71+
72+
func colorBorder(grid [][]int, row, col, color int) [][]int {
73+
m, n := len(grid), len(grid[0])
74+
dirs := []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
75+
vis := make([][]bool, m)
76+
for i := range vis {
77+
vis[i] = make([]bool, n)
78+
}
79+
var borders []point
80+
gInfo := gridInfo{
81+
m: m,
82+
n: n,
83+
grid: grid,
84+
originalColor: grid[row][col],
85+
}
86+
dfs(row, col, gInfo, dirs, vis, &borders)
87+
for _, p := range borders {
88+
grid[p.x][p.y] = color
89+
}
90+
return grid
91+
}
92+
93+
func dfs(x, y int, gInfo gridInfo, dirs []point, vis [][]bool, borders *[]point) {
94+
vis[x][y] = true
95+
isBorder := false
96+
for _, dir := range dirs {
97+
nx, ny := x+dir.x, y+dir.y
98+
if !(0 <= nx && nx < gInfo.m && 0 <= ny && ny < gInfo.n && gInfo.grid[nx][ny] == gInfo.originalColor) {
99+
isBorder = true
100+
} else if !vis[nx][ny] {
101+
dfs(nx, ny, gInfo, dirs, vis, borders)
102+
}
103+
}
104+
if isBorder {
105+
*borders = append(*borders, point{x, y})
106+
}
107+
}
108+
```
109+
110+
111+
----------------------------------------------
112+
<div style="display: flex;justify-content: space-between;align-items: center;">
113+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order/">⬅️上一页</a></p>
114+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1037.Valid-Boomerang/">下一页➡️</a></p>
115+
</div>

website/content/ChapterFour/1000~1099/1037.Valid-Boomerang.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ func isBoomerang(points [][]int) bool {
5151

5252
----------------------------------------------
5353
<div style="display: flex;justify-content: space-between;align-items: center;">
54-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order/">⬅️上一页</a></p>
54+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1034.Coloring-A-Border/">⬅️上一页</a></p>
5555
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1038.Binary-Search-Tree-to-Greater-Sum-Tree/">下一页➡️</a></p>
5656
</div>

website/content/ChapterTwo/Array.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ weight: 1
211211
|0775|Global and Local Inversions|[Go]({{< relref "/ChapterFour/0700~0799/0775.Global-and-Local-Inversions.md" >}})|Medium||||45.7%|
212212
|0778|Swim in Rising Water|[Go]({{< relref "/ChapterFour/0700~0799/0778.Swim-in-Rising-Water.md" >}})|Hard||||58.2%|
213213
|0786|K-th Smallest Prime Fraction|[Go]({{< relref "/ChapterFour/0700~0799/0786.K-th-Smallest-Prime-Fraction.md" >}})|Hard||||47.4%|
214+
|0794|Valid Tic-Tac-Toe State|[Go]({{< relref "/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State.md" >}})|Medium||||35.0%|
214215
|0795|Number of Subarrays with Bounded Maximum|[Go]({{< relref "/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum.md" >}})|Medium||||52.2%|
215216
|0803|Bricks Falling When Hit|[Go]({{< relref "/ChapterFour/0800~0899/0803.Bricks-Falling-When-Hit.md" >}})|Hard||||33.4%|
216217
|0810|Chalkboard XOR Game|[Go]({{< relref "/ChapterFour/0800~0899/0810.Chalkboard-XOR-Game.md" >}})|Hard||||52.3%|
@@ -283,6 +284,7 @@ weight: 1
283284
|1019|Next Greater Node In Linked List|[Go]({{< relref "/ChapterFour/1000~1099/1019.Next-Greater-Node-In-Linked-List.md" >}})|Medium||||59.3%|
284285
|1020|Number of Enclaves|[Go]({{< relref "/ChapterFour/1000~1099/1020.Number-of-Enclaves.md" >}})|Medium||||60.9%|
285286
|1030|Matrix Cells in Distance Order|[Go]({{< relref "/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order.md" >}})|Easy||||68.8%|
287+
|1034|Coloring A Border|[Go]({{< relref "/ChapterFour/1000~1099/1034.Coloring-A-Border.md" >}})|Medium||||47.7%|
286288
|1040|Moving Stones Until Consecutive II|[Go]({{< relref "/ChapterFour/1000~1099/1040.Moving-Stones-Until-Consecutive-II.md" >}})|Medium||||55.1%|
287289
|1048|Longest String Chain|[Go]({{< relref "/ChapterFour/1000~1099/1048.Longest-String-Chain.md" >}})|Medium||||57.3%|
288290
|1049|Last Stone Weight II|[Go]({{< relref "/ChapterFour/1000~1099/1049.Last-Stone-Weight-II.md" >}})|Medium||||49.9%|

website/content/ChapterTwo/Breadth_First_Search.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ weight: 10
7575
|0987|Vertical Order Traversal of a Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0987.Vertical-Order-Traversal-of-a-Binary-Tree.md" >}})|Hard||||40.3%|
7676
|0993|Cousins in Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0993.Cousins-in-Binary-Tree.md" >}})|Easy| O(n)| O(1)||53.6%|
7777
|1020|Number of Enclaves|[Go]({{< relref "/ChapterFour/1000~1099/1020.Number-of-Enclaves.md" >}})|Medium||||60.9%|
78+
|1034|Coloring A Border|[Go]({{< relref "/ChapterFour/1000~1099/1034.Coloring-A-Border.md" >}})|Medium||||47.7%|
7879
|1091|Shortest Path in Binary Matrix|[Go]({{< relref "/ChapterFour/1000~1099/1091.Shortest-Path-in-Binary-Matrix.md" >}})|Medium||||41.6%|
7980
|1123|Lowest Common Ancestor of Deepest Leaves|[Go]({{< relref "/ChapterFour/1100~1199/1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md" >}})|Medium||||69.3%|
8081
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||51.5%|

0 commit comments

Comments
 (0)