Update 794 solution

This commit is contained in:
halfrost
2021-12-21 21:21:21 +08:00
parent f6f2eeb119
commit 7930b8dbf7
15 changed files with 257 additions and 11 deletions

View File

@ -1,4 +1,4 @@
# [794. Valid Tic-Tac-Toe State](https://leetcode-cn.com/problems/valid-tic-tac-toe-state/)
# [794. Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/)
## 题目
@ -71,7 +71,7 @@ Here are the rules of Tic-Tac-Toe:
分类模拟:
- 根据题意棋盘在任意时候,要么 X 的数量比 O 的数量多 1要么两者相等
- X 的数量等于 O 的数量时,任何行、列或对角线都不会出现 3 个相同的 X
- X 的数量比 O 的数量多 1时,任何行、列或对角线都不会出现 3 个相同的 O
- X 的数量比 O 的数量多 1 时,任何行、列或对角线都不会出现 3 个相同的 O
## 代码

View File

@ -1,4 +1,4 @@
# [1034. Coloring A Border](https://leetcode-cn.com/problems/coloring-a-border/)
# [1034. Coloring A Border](https://leetcode.com/problems/coloring-a-border/)
## 题目
@ -50,7 +50,7 @@ Return the final grid.
## 解题思路
- bfs进行遍历选出边界使用color给边界着色
- bfs 进行遍历选出边界使用 color 给边界着色
## 代码

View File

@ -104,5 +104,5 @@ func preimageSizeFZF1(K int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State/">下一页➡️</a></p>
</div>

View File

@ -0,0 +1,126 @@
# [794. Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/)
## 题目
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**:
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe1-grid.jpg)
Input: board = ["O "," "," "]
Output: false
Explanation: The first player always plays "X".
**Example 2**:
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe2-grid.jpg)
Input: board = ["XOX"," X "," "]
Output: false
Explanation: Players take turns making moves.
**Example 3**:
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe3-grid.jpg)
Input: board = ["XXX"," ","OOO"]
Output: false
**Example 4**:
![https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg](https://assets.leetcode.com/uploads/2021/05/15/tictactoe4-grid.jpg)
Input: board = ["XOX","O O","XOX"]
Output: true
**Constraints:**
- board.length == 3
- board[i].length == 3
- board[i][j] is either 'X', 'O', or ' '.
## 题目大意
给你一个字符串数组 board 表示井字游戏的棋盘。当且仅当在井字游戏过程中,棋盘有可能达到 board 所显示的状态时,才返回 true 。
井字游戏的棋盘是一个 3 x 3 数组,由字符 ' ''X' 和 'O' 组成。字符 ' ' 代表一个空位。
以下是井字游戏的规则:
- 玩家轮流将字符放入空位(' ')中。
- 玩家 1 总是放字符 'X' ,而玩家 2 总是放字符 'O' 。
- 'X' 和 'O' 只允许放置在空位中,不允许对已放有字符的位置进行填充。
- 当有 3 个相同(且非空)的字符填充任何行、列或对角线时,游戏结束。
- 当所有位置非空时,也算为游戏结束。
- 如果游戏结束,玩家不允许再放置字符。
## 解题思路
分类模拟:
- 根据题意棋盘在任意时候,要么 X 的数量比 O 的数量多 1要么两者相等
- X 的数量等于 O 的数量时,任何行、列或对角线都不会出现 3 个相同的 X
- X 的数量比 O 的数量多 1 时,任何行、列或对角线都不会出现 3 个相同的 O
## 代码
```go
package leetcode
func validTicTacToe(board []string) bool {
cntX, cntO := 0, 0
for i := range board {
for j := range board[i] {
if board[i][j] == 'X' {
cntX++
} else if board[i][j] == 'O' {
cntO++
}
}
}
if cntX < cntO || cntX > cntO+1 {
return false
}
if cntX == cntO {
return process(board, 'X')
}
return process(board, 'O')
}
func process(board []string, c byte) bool {
//某一行是"ccc"
if board[0] == string([]byte{c, c, c}) || board[1] == string([]byte{c, c, c}) || board[2] == string([]byte{c, c, c}) {
return false
}
//某一列是"ccc"
if (board[0][0] == c && board[1][0] == c && board[2][0] == c) ||
(board[0][1] == c && board[1][1] == c && board[2][1] == c) ||
(board[0][2] == c && board[1][2] == c && board[2][2] == c) {
return false
}
//某一对角线是"ccc"
if (board[0][0] == c && board[1][1] == c && board[2][2] == c) ||
(board[0][2] == c && board[1][1] == c && board[2][0] == c) {
return false
}
return true
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0795.Number-of-Subarrays-with-Bounded-Maximum/">下一页➡️</a></p>
</div>

View File

@ -56,6 +56,6 @@ func getAnswerPerBound(nums []int, bound int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0793.Preimage-Size-of-Factorial-Zeroes-Function/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0802.Find-Eventual-Safe-States/">下一页➡️</a></p>
</div>

View File

@ -87,5 +87,5 @@ func allCellsDistOrder(R int, C int, r0 int, c0 int) [][]int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1028.Recover-a-Tree-From-Preorder-Traversal/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1037.Valid-Boomerang/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1034.Coloring-A-Border/">下一页➡️</a></p>
</div>

View File

@ -0,0 +1,115 @@
# [1034. Coloring A Border](https://leetcode.com/problems/coloring-a-border/)
## 题目
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.
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.
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).
You should color the border of the connected component that contains the square grid[row][col] with color.
Return the final grid.
**Example 1**:
Input: grid = [[1,1],[1,2]], row = 0, col = 0, color = 3
Output: [[3,3],[3,2]]
**Example 2**:
Input: grid = [[1,2,2],[2,3,2]], row = 0, col = 1, color = 3
Output: [[1,3,3],[2,3,3]]
**Example 3**:
Input: grid = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1, color = 2
Output: [[2,2,2],[2,1,2],[2,2,2]]
**Constraints:**
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 50
- 1 <= grid[i][j], color <= 1000
- 0 <= row < m
- 0 <= col < n
## 题目大意
给你一个大小为 m x n 的整数矩阵 grid 表示一个网格另给你三个整数 rowcol color 网格中的每个值表示该位置处的网格块的颜色
当两个网格块的颜色相同而且在四个方向中任意一个方向上相邻时它们属于同一连通分量
边界在连通分量的块中前提并且满足以下条件之一
1要么上下左右存在一个块不在连通分量里面
2要么这个块的位置在整个grid的边框上
请你使用指定颜色 color 为所有包含网格块 grid[row][col] 的连通分量的边界进行着色并返回最终的网格 grid
## 解题思路
- bfs 进行遍历选出边界使用 color 给边界着色
## 代码
```go
package leetcode
type point struct {
x int
y int
}
type gridInfo struct {
m int
n int
grid [][]int
originalColor int
}
func colorBorder(grid [][]int, row, col, color int) [][]int {
m, n := len(grid), len(grid[0])
dirs := []point{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
vis := make([][]bool, m)
for i := range vis {
vis[i] = make([]bool, n)
}
var borders []point
gInfo := gridInfo{
m: m,
n: n,
grid: grid,
originalColor: grid[row][col],
}
dfs(row, col, gInfo, dirs, vis, &borders)
for _, p := range borders {
grid[p.x][p.y] = color
}
return grid
}
func dfs(x, y int, gInfo gridInfo, dirs []point, vis [][]bool, borders *[]point) {
vis[x][y] = true
isBorder := false
for _, dir := range dirs {
nx, ny := x+dir.x, y+dir.y
if !(0 <= nx && nx < gInfo.m && 0 <= ny && ny < gInfo.n && gInfo.grid[nx][ny] == gInfo.originalColor) {
isBorder = true
} else if !vis[nx][ny] {
dfs(nx, ny, gInfo, dirs, vis, borders)
}
}
if isBorder {
*borders = append(*borders, point{x, y})
}
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1037.Valid-Boomerang/">下一页➡️</a></p>
</div>

View File

@ -51,6 +51,6 @@ func isBoomerang(points [][]int) bool {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1030.Matrix-Cells-in-Distance-Order/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1034.Coloring-A-Border/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/1000~1099/1038.Binary-Search-Tree-to-Greater-Sum-Tree/">下一页➡️</a></p>
</div>

View File

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

View File

@ -75,6 +75,7 @@ weight: 10
|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%|
|0993|Cousins in Binary Tree|[Go]({{< relref "/ChapterFour/0900~0999/0993.Cousins-in-Binary-Tree.md" >}})|Easy| O(n)| O(1)||53.6%|
|1020|Number of Enclaves|[Go]({{< relref "/ChapterFour/1000~1099/1020.Number-of-Enclaves.md" >}})|Medium||||60.9%|
|1034|Coloring A Border|[Go]({{< relref "/ChapterFour/1000~1099/1034.Coloring-A-Border.md" >}})|Medium||||47.7%|
|1091|Shortest Path in Binary Matrix|[Go]({{< relref "/ChapterFour/1000~1099/1091.Shortest-Path-in-Binary-Matrix.md" >}})|Medium||||41.6%|
|1123|Lowest Common Ancestor of Deepest Leaves|[Go]({{< relref "/ChapterFour/1100~1199/1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md" >}})|Medium||||69.3%|
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||51.5%|

View File

@ -99,6 +99,7 @@ weight: 9
|1020|Number of Enclaves|[Go]({{< relref "/ChapterFour/1000~1099/1020.Number-of-Enclaves.md" >}})|Medium||||60.9%|
|1026|Maximum Difference Between Node and Ancestor|[Go]({{< relref "/ChapterFour/1000~1099/1026.Maximum-Difference-Between-Node-and-Ancestor.md" >}})|Medium||||70.9%|
|1028|Recover a Tree From Preorder Traversal|[Go]({{< relref "/ChapterFour/1000~1099/1028.Recover-a-Tree-From-Preorder-Traversal.md" >}})|Hard||||72.0%|
|1034|Coloring A Border|[Go]({{< relref "/ChapterFour/1000~1099/1034.Coloring-A-Border.md" >}})|Medium||||47.7%|
|1038|Binary Search Tree to Greater Sum Tree|[Go]({{< relref "/ChapterFour/1000~1099/1038.Binary-Search-Tree-to-Greater-Sum-Tree.md" >}})|Medium||||84.0%|
|1110|Delete Nodes And Return Forest|[Go]({{< relref "/ChapterFour/1100~1199/1110.Delete-Nodes-And-Return-Forest.md" >}})|Medium||||68.9%|
|1123|Lowest Common Ancestor of Deepest Leaves|[Go]({{< relref "/ChapterFour/1100~1199/1123.Lowest-Common-Ancestor-of-Deepest-Leaves.md" >}})|Medium||||69.3%|

View File

@ -39,7 +39,7 @@ weight: 4
|0138|Copy List with Random Pointer|[Go]({{< relref "/ChapterFour/0100~0199/0138.Copy-List-with-Random-Pointer.md" >}})|Medium||||45.3%|
|0141|Linked List Cycle|[Go]({{< relref "/ChapterFour/0100~0199/0141.Linked-List-Cycle.md" >}})|Easy| O(n)| O(1)|❤️|44.6%|
|0142|Linked List Cycle II|[Go]({{< relref "/ChapterFour/0100~0199/0142.Linked-List-Cycle-II.md" >}})|Medium| O(n)| O(1)|❤️|42.2%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium| O(n)| O(1)|❤️|45.6%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium| O(n)| O(1)|❤️|45.7%|
|0146|LRU Cache|[Go]({{< relref "/ChapterFour/0100~0199/0146.LRU-Cache.md" >}})|Medium||||38.8%|
|0147|Insertion Sort List|[Go]({{< relref "/ChapterFour/0100~0199/0147.Insertion-Sort-List.md" >}})|Medium| O(n)| O(1)|❤️|48.0%|
|0148|Sort List|[Go]({{< relref "/ChapterFour/0100~0199/0148.Sort-List.md" >}})|Medium| O(n log n)| O(n)|❤️|49.4%|

View File

@ -24,7 +24,7 @@ weight: 5
|0084|Largest Rectangle in Histogram|[Go]({{< relref "/ChapterFour/0001~0099/0084.Largest-Rectangle-in-Histogram.md" >}})|Hard| O(n)| O(n)|❤️|39.3%|
|0094|Binary Tree Inorder Traversal|[Go]({{< relref "/ChapterFour/0001~0099/0094.Binary-Tree-Inorder-Traversal.md" >}})|Easy| O(n)| O(1)||69.3%|
|0114|Flatten Binary Tree to Linked List|[Go]({{< relref "/ChapterFour/0100~0199/0114.Flatten-Binary-Tree-to-Linked-List.md" >}})|Medium||||56.2%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium||||45.6%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium||||45.7%|
|0144|Binary Tree Preorder Traversal|[Go]({{< relref "/ChapterFour/0100~0199/0144.Binary-Tree-Preorder-Traversal.md" >}})|Easy| O(n)| O(1)||60.8%|
|0145|Binary Tree Postorder Traversal|[Go]({{< relref "/ChapterFour/0100~0199/0145.Binary-Tree-Postorder-Traversal.md" >}})|Easy| O(n)| O(1)||62.0%|
|0150|Evaluate Reverse Polish Notation|[Go]({{< relref "/ChapterFour/0100~0199/0150.Evaluate-Reverse-Polish-Notation.md" >}})|Medium| O(n)| O(1)||41.0%|

View File

@ -108,6 +108,7 @@ weight: 2
|0784|Letter Case Permutation|[Go]({{< relref "/ChapterFour/0700~0799/0784.Letter-Case-Permutation.md" >}})|Medium||||71.0%|
|0791|Custom Sort String|[Go]({{< relref "/ChapterFour/0700~0799/0791.Custom-Sort-String.md" >}})|Medium||||68.0%|
|0792|Number of Matching Subsequences|[Go]({{< relref "/ChapterFour/0700~0799/0792.Number-of-Matching-Subsequences.md" >}})|Medium||||50.2%|
|0794|Valid Tic-Tac-Toe State|[Go]({{< relref "/ChapterFour/0700~0799/0794.Valid-Tic-Tac-Toe-State.md" >}})|Medium||||35.0%|
|0811|Subdomain Visit Count|[Go]({{< relref "/ChapterFour/0800~0899/0811.Subdomain-Visit-Count.md" >}})|Medium||||73.4%|
|0816|Ambiguous Coordinates|[Go]({{< relref "/ChapterFour/0800~0899/0816.Ambiguous-Coordinates.md" >}})|Medium||||55.8%|
|0819|Most Common Word|[Go]({{< relref "/ChapterFour/0800~0899/0819.Most-Common-Word.md" >}})|Easy||||45.3%|

View File

@ -51,7 +51,7 @@ weight: 3
|0125|Valid Palindrome|[Go]({{< relref "/ChapterFour/0100~0199/0125.Valid-Palindrome.md" >}})|Easy| O(n)| O(1)||40.3%|
|0141|Linked List Cycle|[Go]({{< relref "/ChapterFour/0100~0199/0141.Linked-List-Cycle.md" >}})|Easy| O(n)| O(1)|❤️|44.6%|
|0142|Linked List Cycle II|[Go]({{< relref "/ChapterFour/0100~0199/0142.Linked-List-Cycle-II.md" >}})|Medium| O(n)| O(1)|❤️|42.2%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium||||45.6%|
|0143|Reorder List|[Go]({{< relref "/ChapterFour/0100~0199/0143.Reorder-List.md" >}})|Medium||||45.7%|
|0148|Sort List|[Go]({{< relref "/ChapterFour/0100~0199/0148.Sort-List.md" >}})|Medium||||49.4%|
|0151|Reverse Words in a String|[Go]({{< relref "/ChapterFour/0100~0199/0151.Reverse-Words-in-a-String.md" >}})|Medium||||27.2%|
|0160|Intersection of Two Linked Lists|[Go]({{< relref "/ChapterFour/0100~0199/0160.Intersection-of-Two-Linked-Lists.md" >}})|Easy||||48.1%|