1. Add solution 1091、1614、1619、1624、1629、1636、1704

2. ctl strings.TrimSpace question.Title
This commit is contained in:
YDZ
2021-02-14 12:30:19 +08:00
parent af41c91d60
commit 14d0942f5a
57 changed files with 2046 additions and 271 deletions

View File

@ -0,0 +1,54 @@
package leetcode
var dir = [][]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{0, -1},
{1, -1},
{1, 0},
{1, 1},
}
func shortestPathBinaryMatrix(grid [][]int) int {
visited := make([][]bool, 0)
for range make([]int, len(grid)) {
visited = append(visited, make([]bool, len(grid[0])))
}
dis := make([][]int, 0)
for range make([]int, len(grid)) {
dis = append(dis, make([]int, len(grid[0])))
}
if grid[0][0] == 1 {
return -1
}
if len(grid) == 1 && len(grid[0]) == 1 {
return 1
}
queue := []int{0}
visited[0][0], dis[0][0] = true, 1
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
curx, cury := cur/len(grid[0]), cur%len(grid[0])
for d := 0; d < 8; d++ {
nextx := curx + dir[d][0]
nexty := cury + dir[d][1]
if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
queue = append(queue, nextx*len(grid[0])+nexty)
visited[nextx][nexty] = true
dis[nextx][nexty] = dis[curx][cury] + 1
if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
return dis[nextx][nexty]
}
}
}
}
return -1
}
func isInBoard(board [][]int, x, y int) bool {
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}

View File

@ -0,0 +1,48 @@
package leetcode
import (
"fmt"
"testing"
)
type question1091 struct {
para1091
ans1091
}
// para 是参数
// one 代表第一个参数
type para1091 struct {
grid [][]int
}
// ans 是答案
// one 代表第一个答案
type ans1091 struct {
one int
}
func Test_Problem1091(t *testing.T) {
qs := []question1091{
{
para1091{[][]int{{0, 1}, {1, 0}}},
ans1091{2},
},
{
para1091{[][]int{{0, 0, 0}, {1, 1, 0}, {1, 1, 0}}},
ans1091{4},
},
}
fmt.Printf("------------------------Leetcode Problem 1091------------------------\n")
for _, q := range qs {
_, p := q.ans1091, q.para1091
fmt.Printf("【input】:%v 【output】:%v\n", p, shortestPathBinaryMatrix(p.grid))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,114 @@
# [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
## 题目
In an N by N square grid, each cell is either empty (0) or blocked (1).
A *clear path from top-left to bottom-right* has length `k` if and only if it is composed of cells `C_1, C_2, ..., C_k` such that:
- Adjacent cells `C_i` and `C_{i+1}` are connected 8-directionally (ie., they are different and share an edge or corner)
- `C_1` is at location `(0, 0)` (ie. has value `grid[0][0]`)
- `C_k` is at location `(N-1, N-1)` (ie. has value `grid[N-1][N-1]`)
- If `C_i` is located at `(r, c)`, then `grid[r][c]` is empty (ie. `grid[r][c] == 0`).
Return the length of the shortest such clear path from top-left to bottom-right.  If such a path does not exist, return -1.
**Example 1:**
```
Input: [[0,1],[1,0]]
Output: 2
```
![https://assets.leetcode.com/uploads/2019/08/04/example1_1.png](https://assets.leetcode.com/uploads/2019/08/04/example1_1.png)
![https://assets.leetcode.com/uploads/2019/08/04/example1_2.png](https://assets.leetcode.com/uploads/2019/08/04/example1_2.png)
**Example 2:**
```
Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
```
![https://assets.leetcode.com/uploads/2019/08/04/example2_1.png](https://assets.leetcode.com/uploads/2019/08/04/example2_1.png)
![https://assets.leetcode.com/uploads/2019/08/04/example2_2.png](https://assets.leetcode.com/uploads/2019/08/04/example2_2.png)
**Note:**
1. `1 <= grid.length == grid[0].length <= 100`
2. `grid[r][c]` is `0` or `1`
## 题目大意
在一个 N × N 的方形网格中每个单元格有两种状态0或者阻塞1。一条从左上角到右下角、长度为 k 的畅通路径由满足下述条件的单元格 C_1, C_2, ..., C_k 组成
- 相邻单元格 C_i 和 C_{i+1} 在八个方向之一上连通此时C_i 和 C_{i+1} 不同且共享边或角)
- C_1 位于 (0, 0)值为 grid[0][0]
- C_k 位于 (N-1, N-1)值为 grid[N-1][N-1]
- 如果 C_i 位于 (r, c),则 grid[r][c] 为空grid[r][c] == 0
返回这条从左上角到右下角的最短畅通路径的长度。如果不存在这样的路径,返回 -1 。
## 解题思路
- 这一题是简单的找最短路径。利用 BFS 从左上角逐步扩展到右下角,便可以很容易求解。注意每轮扩展需要考虑 8 个方向。
## 代码
```go
var dir = [][]int{
{-1, -1},
{-1, 0},
{-1, 1},
{0, 1},
{0, -1},
{1, -1},
{1, 0},
{1, 1},
}
func shortestPathBinaryMatrix(grid [][]int) int {
visited := make([][]bool, 0)
for range make([]int, len(grid)) {
visited = append(visited, make([]bool, len(grid[0])))
}
dis := make([][]int, 0)
for range make([]int, len(grid)) {
dis = append(dis, make([]int, len(grid[0])))
}
if grid[0][0] == 1 {
return -1
}
if len(grid) == 1 && len(grid[0]) == 1 {
return 1
}
queue := []int{0}
visited[0][0], dis[0][0] = true, 1
for len(queue) > 0 {
cur := queue[0]
queue = queue[1:]
curx, cury := cur/len(grid[0]), cur%len(grid[0])
for d := 0; d < 8; d++ {
nextx := curx + dir[d][0]
nexty := cury + dir[d][1]
if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
queue = append(queue, nextx*len(grid[0])+nexty)
visited[nextx][nexty] = true
dis[nextx][nexty] = dis[curx][cury] + 1
if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
return dis[nextx][nexty]
}
}
}
}
return -1
}
func isInBoard(board [][]int, x, y int) bool {
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
}
```