Skip to content

Commit 14d0942

Browse files
committed
1. Add solution 1091、1614、1619、1624、1629、1636、1704
2. ctl strings.TrimSpace question.Title
1 parent af41c91 commit 14d0942

File tree

57 files changed

+2043
-268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2043
-268
lines changed

README.md

Lines changed: 186 additions & 182 deletions
Large diffs are not rendered by default.

ctl/models/lcproblems.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import (
44
"fmt"
5+
"strings"
56
)
67

78
// LeetCodeProblemAll define
@@ -53,8 +54,8 @@ func ConvertMdModelFromSsp(problems []StatStatusPairs) []Mdrow {
5354
for _, problem := range problems {
5455
res := Mdrow{}
5556
res.FrontendQuestionID = problem.Stat.FrontendQuestionID
56-
res.QuestionTitle = problem.Stat.QuestionTitle
57-
res.QuestionTitleSlug = problem.Stat.QuestionTitleSlug
57+
res.QuestionTitle = strings.TrimSpace(problem.Stat.QuestionTitle)
58+
res.QuestionTitleSlug = strings.TrimSpace(problem.Stat.QuestionTitleSlug)
5859
res.Acceptance = fmt.Sprintf("%.1f%%", (problem.Stat.TotalAcs/problem.Stat.TotalSubmitted)*100)
5960
res.Difficulty = DifficultyMap[problem.Difficulty.Level]
6061
res.Frequency = fmt.Sprintf("%f", problem.Frequency)
@@ -69,8 +70,8 @@ func ConvertMdModelFromIds(problemsMap map[int]StatStatusPairs, ids []int) []Mdr
6970
for _, v := range ids {
7071
res, problem := Mdrow{}, problemsMap[v]
7172
res.FrontendQuestionID = problem.Stat.FrontendQuestionID
72-
res.QuestionTitle = problem.Stat.QuestionTitle
73-
res.QuestionTitleSlug = problem.Stat.QuestionTitleSlug
73+
res.QuestionTitle = strings.TrimSpace(problem.Stat.QuestionTitle)
74+
res.QuestionTitleSlug = strings.TrimSpace(problem.Stat.QuestionTitleSlug)
7475
res.Acceptance = fmt.Sprintf("%.1f%%", (problem.Stat.TotalAcs/problem.Stat.TotalSubmitted)*100)
7576
res.Difficulty = DifficultyMap[problem.Difficulty.Level]
7677
res.Frequency = fmt.Sprintf("%f", problem.Frequency)

ctl/models/mdrow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func GenerateMdRows(solutionIds []int, mdrows []Mdrow) {
2222
id := mdrows[solutionIds[i]-1].FrontendQuestionID
2323
if solutionIds[i] == int(id) {
2424
//fmt.Printf("id = %v i = %v solutionIds = %v\n", id, i, solutionIds[i])
25-
mdrows[id-1].SolutionPath = fmt.Sprintf("[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/%v)", fmt.Sprintf("%04d.%v", id, strings.Replace(mdrows[id-1].QuestionTitle, " ", "-", -1)))
25+
mdrows[id-1].SolutionPath = fmt.Sprintf("[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/%v)", fmt.Sprintf("%04d.%v", id, strings.Replace(strings.TrimSpace(mdrows[id-1].QuestionTitle), " ", "-", -1)))
2626
} else {
2727
fmt.Printf("序号出错了 solutionIds = %v id = %v\n", solutionIds[i], id)
2828
}

ctl/models/tagproblem.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package models
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/halfrost/LeetCode-Go/ctl/util"
76
"strconv"
87
"strings"
8+
9+
"github.com/halfrost/LeetCode-Go/ctl/util"
910
)
1011

1112
// Graphql define
@@ -84,8 +85,8 @@ func ConvertMdModelFromQuestions(questions []Question) []Mdrow {
8485
res := Mdrow{}
8586
v, _ := strconv.Atoi(question.QuestionFrontendID)
8687
res.FrontendQuestionID = int32(v)
87-
res.QuestionTitle = question.Title
88-
res.QuestionTitleSlug = question.TitleSlug
88+
res.QuestionTitle = strings.TrimSpace(question.Title)
89+
res.QuestionTitleSlug = strings.TrimSpace(question.TitleSlug)
8990
q, err := question.generateTagStatus()
9091
if err != nil {
9192
fmt.Println(err)
@@ -121,8 +122,8 @@ func GenerateTagMdRows(solutionIds []int, metaMap map[int]TagList, mdrows []Mdro
121122
if util.BinarySearch(solutionIds, int(row.FrontendQuestionID)) != -1 {
122123
tmp := TagList{}
123124
tmp.FrontendQuestionID = row.FrontendQuestionID
124-
tmp.QuestionTitle = row.QuestionTitle
125-
s1 := strings.Replace(row.QuestionTitle, " ", "-", -1)
125+
tmp.QuestionTitle = strings.TrimSpace(row.QuestionTitle)
126+
s1 := strings.Replace(tmp.QuestionTitle, " ", "-", -1)
126127
s2 := strings.Replace(s1, "'", "", -1)
127128
s3 := strings.Replace(s2, "%", "", -1)
128129
s4 := strings.Replace(s3, "(", "", -1)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode
2+
3+
var dir = [][]int{
4+
{-1, -1},
5+
{-1, 0},
6+
{-1, 1},
7+
{0, 1},
8+
{0, -1},
9+
{1, -1},
10+
{1, 0},
11+
{1, 1},
12+
}
13+
14+
func shortestPathBinaryMatrix(grid [][]int) int {
15+
visited := make([][]bool, 0)
16+
for range make([]int, len(grid)) {
17+
visited = append(visited, make([]bool, len(grid[0])))
18+
}
19+
dis := make([][]int, 0)
20+
for range make([]int, len(grid)) {
21+
dis = append(dis, make([]int, len(grid[0])))
22+
}
23+
if grid[0][0] == 1 {
24+
return -1
25+
}
26+
if len(grid) == 1 && len(grid[0]) == 1 {
27+
return 1
28+
}
29+
30+
queue := []int{0}
31+
visited[0][0], dis[0][0] = true, 1
32+
for len(queue) > 0 {
33+
cur := queue[0]
34+
queue = queue[1:]
35+
curx, cury := cur/len(grid[0]), cur%len(grid[0])
36+
for d := 0; d < 8; d++ {
37+
nextx := curx + dir[d][0]
38+
nexty := cury + dir[d][1]
39+
if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
40+
queue = append(queue, nextx*len(grid[0])+nexty)
41+
visited[nextx][nexty] = true
42+
dis[nextx][nexty] = dis[curx][cury] + 1
43+
if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
44+
return dis[nextx][nexty]
45+
}
46+
}
47+
}
48+
}
49+
return -1
50+
}
51+
52+
func isInBoard(board [][]int, x, y int) bool {
53+
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
54+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1091 struct {
9+
para1091
10+
ans1091
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1091 struct {
16+
grid [][]int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1091 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1091(t *testing.T) {
26+
27+
qs := []question1091{
28+
29+
{
30+
para1091{[][]int{{0, 1}, {1, 0}}},
31+
ans1091{2},
32+
},
33+
34+
{
35+
para1091{[][]int{{0, 0, 0}, {1, 1, 0}, {1, 1, 0}}},
36+
ans1091{4},
37+
},
38+
}
39+
40+
fmt.Printf("------------------------Leetcode Problem 1091------------------------\n")
41+
42+
for _, q := range qs {
43+
_, p := q.ans1091, q.para1091
44+
fmt.Printf("【input】:%v 【output】:%v\n", p, shortestPathBinaryMatrix(p.grid))
45+
46+
}
47+
fmt.Printf("\n\n\n")
48+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
2+
3+
4+
## 题目
5+
6+
In an N by N square grid, each cell is either empty (0) or blocked (1).
7+
8+
*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:
9+
10+
- Adjacent cells `C_i` and `C_{i+1}` are connected 8-directionally (ie., they are different and share an edge or corner)
11+
- `C_1` is at location `(0, 0)` (ie. has value `grid[0][0]`)
12+
- `C_k` is at location `(N-1, N-1)` (ie. has value `grid[N-1][N-1]`)
13+
- If `C_i` is located at `(r, c)`, then `grid[r][c]` is empty (ie. `grid[r][c] == 0`).
14+
15+
Return the length of the shortest such clear path from top-left to bottom-right.  If such a path does not exist, return -1.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: [[0,1],[1,0]]
21+
Output: 2
22+
```
23+
24+
![https://assets.leetcode.com/uploads/2019/08/04/example1_1.png](https://assets.leetcode.com/uploads/2019/08/04/example1_1.png)
25+
26+
![https://assets.leetcode.com/uploads/2019/08/04/example1_2.png](https://assets.leetcode.com/uploads/2019/08/04/example1_2.png)
27+
28+
**Example 2:**
29+
30+
```
31+
Input: [[0,0,0],[1,1,0],[1,1,0]]
32+
Output: 4
33+
```
34+
35+
![https://assets.leetcode.com/uploads/2019/08/04/example2_1.png](https://assets.leetcode.com/uploads/2019/08/04/example2_1.png)
36+
37+
![https://assets.leetcode.com/uploads/2019/08/04/example2_2.png](https://assets.leetcode.com/uploads/2019/08/04/example2_2.png)
38+
39+
**Note:**
40+
41+
1. `1 <= grid.length == grid[0].length <= 100`
42+
2. `grid[r][c]` is `0` or `1`
43+
44+
## 题目大意
45+
46+
在一个 N × N 的方形网格中,每个单元格有两种状态:空(0)或者阻塞(1)。一条从左上角到右下角、长度为 k 的畅通路径,由满足下述条件的单元格 C_1, C_2, ..., C_k 组成:
47+
48+
- 相邻单元格 C_i 和 C_{i+1} 在八个方向之一上连通(此时,C_i 和 C_{i+1} 不同且共享边或角)
49+
- C_1 位于 (0, 0)(即,值为 grid[0][0]
50+
- C_k 位于 (N-1, N-1)(即,值为 grid[N-1][N-1]
51+
- 如果 C_i 位于 (r, c),则 grid[r][c] 为空(即,grid[r][c] == 0)
52+
53+
返回这条从左上角到右下角的最短畅通路径的长度。如果不存在这样的路径,返回 -1 。
54+
55+
## 解题思路
56+
57+
- 这一题是简单的找最短路径。利用 BFS 从左上角逐步扩展到右下角,便可以很容易求解。注意每轮扩展需要考虑 8 个方向。
58+
59+
## 代码
60+
61+
```go
62+
var dir = [][]int{
63+
{-1, -1},
64+
{-1, 0},
65+
{-1, 1},
66+
{0, 1},
67+
{0, -1},
68+
{1, -1},
69+
{1, 0},
70+
{1, 1},
71+
}
72+
73+
func shortestPathBinaryMatrix(grid [][]int) int {
74+
visited := make([][]bool, 0)
75+
for range make([]int, len(grid)) {
76+
visited = append(visited, make([]bool, len(grid[0])))
77+
}
78+
dis := make([][]int, 0)
79+
for range make([]int, len(grid)) {
80+
dis = append(dis, make([]int, len(grid[0])))
81+
}
82+
if grid[0][0] == 1 {
83+
return -1
84+
}
85+
if len(grid) == 1 && len(grid[0]) == 1 {
86+
return 1
87+
}
88+
89+
queue := []int{0}
90+
visited[0][0], dis[0][0] = true, 1
91+
for len(queue) > 0 {
92+
cur := queue[0]
93+
queue = queue[1:]
94+
curx, cury := cur/len(grid[0]), cur%len(grid[0])
95+
for d := 0; d < 8; d++ {
96+
nextx := curx + dir[d][0]
97+
nexty := cury + dir[d][1]
98+
if isInBoard(grid, nextx, nexty) && !visited[nextx][nexty] && grid[nextx][nexty] == 0 {
99+
queue = append(queue, nextx*len(grid[0])+nexty)
100+
visited[nextx][nexty] = true
101+
dis[nextx][nexty] = dis[curx][cury] + 1
102+
if nextx == len(grid)-1 && nexty == len(grid[0])-1 {
103+
return dis[nextx][nexty]
104+
}
105+
}
106+
}
107+
}
108+
return -1
109+
}
110+
111+
func isInBoard(board [][]int, x, y int) bool {
112+
return x >= 0 && x < len(board) && y >= 0 && y < len(board[0])
113+
}
114+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func maxDepth(s string) int {
4+
res, cur := 0, 0
5+
for _, c := range s {
6+
if c == '(' {
7+
cur++
8+
res = max(res, cur)
9+
} else if c == ')' {
10+
cur--
11+
}
12+
}
13+
return res
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1614 struct {
9+
para1614
10+
ans1614
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1614 struct {
16+
p string
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1614 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1614(t *testing.T) {
26+
27+
qs := []question1614{
28+
29+
{
30+
para1614{"(1+(2*3)+((8)/4))+1"},
31+
ans1614{3},
32+
},
33+
34+
{
35+
para1614{"(1)+((2))+(((3)))"},
36+
ans1614{3},
37+
},
38+
39+
{
40+
para1614{"1+(2*3)/(2-1)"},
41+
ans1614{1},
42+
},
43+
44+
{
45+
para1614{"1"},
46+
ans1614{0},
47+
},
48+
}
49+
50+
fmt.Printf("------------------------Leetcode Problem 1614------------------------\n")
51+
52+
for _, q := range qs {
53+
_, p := q.ans1614, q.para1614
54+
fmt.Printf("【input】:%v 【output】:%v \n", p, maxDepth(p.p))
55+
}
56+
fmt.Printf("\n\n\n")
57+
}

0 commit comments

Comments
 (0)