Skip to content

Commit cd83d78

Browse files
committed
Update test.go
1 parent b836c19 commit cd83d78

11 files changed

Lines changed: 10752 additions & 28 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"editor.insertSpaces": false,
99
"editor.formatOnSave": true,
1010
"editor.codeActionsOnSave": {
11-
"source.organizeImports": true
11+
"source.organizeImports": "explicit"
1212
}
1313
}
1414
}

coverage.txt

Lines changed: 10635 additions & 0 deletions
Large diffs are not rendered by default.

leetcode/0215.Kth-Largest-Element-in-an-Array/215. Kth Largest Element in an Array_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ type ans215 struct {
2323
one int
2424
}
2525

26+
// clone215 复制切片,避免多个就地改写的解法共享同一底层数组。
27+
func clone215(a []int) []int {
28+
return append([]int(nil), a...)
29+
}
30+
2631
func Test_Problem215(t *testing.T) {
2732

2833
qs := []question215{
@@ -61,10 +66,19 @@ func Test_Problem215(t *testing.T) {
6166
fmt.Printf("------------------------Leetcode Problem 215------------------------\n")
6267

6368
for _, q := range qs {
64-
_, p := q.ans215, q.para215
65-
fmt.Printf("【input】:%v 【output】:%v\n", p.one, findKthLargest(p.one, p.two))
66-
findKthLargest1(p.one, p.two)
69+
a, p := q.ans215, q.para215
70+
// findKthLargest(快速选择)、findKthLargest1(排序)、getLeastNumbers 都会
71+
// 就地改写传入切片,所以各给一份独立副本,互不污染。
72+
got := findKthLargest(clone215(p.one), p.two)
73+
got1 := findKthLargest1(clone215(p.one), p.two)
74+
fmt.Printf("【input】:%v 【output】:%v\n", p.one, got)
75+
if got != a.one || got1 != a.one {
76+
t.Fatalf("input %v, k=%v: expected %v, got %v / %v", p.one, p.two, a.one, got, got1)
77+
}
78+
// getLeastNumbers 是扩展题(最小的 k 个数),返回前 k 小,校验长度即可。
79+
if least := getLeastNumbers(clone215(p.one), p.two); len(least) != p.two {
80+
t.Fatalf("getLeastNumbers(%v, %v) len = %v, want %v", p.one, p.two, len(least), p.two)
81+
}
6782
}
6883
fmt.Printf("\n\n\n")
69-
getLeastNumbers(p.one, p.two)
7084
}

leetcode/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ func findBottomLeftValueDFS(root *TreeNode, curHeight int, res, maxHeight *int)
4141

4242
// 解法二 BFS
4343
func findBottomLeftValue1(root *TreeNode) int {
44-
queue := []*TreeNode{root}
44+
if root == nil {
45+
return 0
46+
}
47+
res, queue := root.Val, []*TreeNode{root}
4548
for len(queue) > 0 {
49+
res = queue[0].Val // 每层第一个节点即该层最左节点,最后停留的就是底层最左值
4650
next := []*TreeNode{}
4751
for _, node := range queue {
4852
if node.Left != nil {
@@ -52,10 +56,7 @@ func findBottomLeftValue1(root *TreeNode) int {
5256
next = append(next, node.Right)
5357
}
5458
}
55-
if len(next) == 0 {
56-
return queue[0].Val
57-
}
5859
queue = next
5960
}
60-
return 0
61+
return res
6162
}

leetcode/0513.Find-Bottom-Left-Tree-Value/513. Find Bottom Left Tree Value_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ func Test_Problem513(t *testing.T) {
5252
fmt.Printf("------------------------Leetcode Problem 513------------------------\n")
5353

5454
for _, q := range qs {
55-
_, p := q.ans513, q.para513
55+
a, p := q.ans513, q.para513
5656
fmt.Printf("【input】:%v ", p)
5757
root := structures.Ints2TreeNode(p.one)
58-
fmt.Printf("【output】:%v \n", findBottomLeftValue(root))
59-
findBottomLeftValue1(root)
58+
got := findBottomLeftValue(root)
59+
fmt.Printf("【output】:%v \n", got)
60+
got1 := findBottomLeftValue1(root)
61+
if got != a.one || got1 != a.one {
62+
t.Fatalf("input %v: expected %v, DFS got %v, BFS got %v", p.one, a.one, got, got1)
63+
}
6064
}
6165
fmt.Printf("\n\n\n")
6266
}

leetcode/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ func max(a int, b int) int {
3030

3131
// 解法二 优化辅助空间的 DP
3232
func maxProfit714_1(prices []int, fee int) int {
33+
if len(prices) <= 1 {
34+
return 0
35+
}
3336
sell, buy := 0, -prices[0]
3437
for i := 1; i < len(prices); i++ {
3538
sell = max(sell, buy+prices[i]-fee)

leetcode/0714.Best-Time-to-Buy-and-Sell-Stock-with-Transaction-Fee/714. Best Time to Buy and Sell Stock with Transaction Fee_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ func Test_Problem714(t *testing.T) {
5151
fmt.Printf("------------------------Leetcode Problem 714------------------------\n")
5252

5353
for _, q := range qs {
54-
_, p := q.ans714, q.para714
55-
fmt.Printf("【input】:%v 【output】:%v\n", p, maxProfit714(p.one, p.f))
56-
maxProfit714_1(p.one, p.f)
54+
a, p := q.ans714, q.para714
55+
got := maxProfit714(p.one, p.f)
56+
fmt.Printf("【input】:%v 【output】:%v\n", p, got)
57+
got1 := maxProfit714_1(p.one, p.f)
58+
if got != a.one || got1 != a.one {
59+
t.Fatalf("input %v, fee=%v: expected %v, got %v / %v", p.one, p.f, a.one, got, got1)
60+
}
5761
}
5862
fmt.Printf("\n\n\n")
5963
}

leetcode/0719.Find-K-th-Smallest-Pair-Distance/719. Find K-th Smallest Pair Distance_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,16 @@ func Test_Problem719(t *testing.T) {
5656
fmt.Printf("------------------------Leetcode Problem 719------------------------\n")
5757

5858
for _, q := range qs {
59-
_, p := q.ans719, q.para719
60-
fmt.Printf("【input】:%v 【output】:%v\n", p, smallestDistancePair(p.num, p.k))
59+
a, p := q.ans719, q.para719
60+
got := smallestDistancePair(p.num, p.k) // 会把 p.num 排序
61+
fmt.Printf("【input】:%v 【output】:%v\n", p, got)
62+
if got != a.one {
63+
t.Fatalf("input %v, k=%v: expected %v, got %v", p.num, p.k, a.one, got)
64+
}
65+
// 双指针解法与暴力解法在已排序数组上对同一距离阈值的计数应当一致。
66+
if c1, c2 := findDistanceCount(p.num, a.one), findDistanceCount1(p.num, a.one); c1 != c2 {
67+
t.Fatalf("distance count mismatch for %v, num=%v: %v vs %v", p.num, a.one, c1, c2)
68+
}
6169
}
6270
fmt.Printf("\n\n\n")
63-
findDistanceCount1(p.num, p.k)
6471
}

leetcode/0864.Shortest-Path-to-Get-All-Keys/864. Shortest Path to Get All Keys_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,30 @@ func Test_Problem864(t *testing.T) {
6565
para864{[]string{"@..aA", "..B#.", "....b"}},
6666
ans864{6},
6767
},
68+
69+
{
70+
para864{[]string{}},
71+
ans864{0},
72+
},
6873
}
6974

7075
fmt.Printf("------------------------Leetcode Problem 864------------------------\n")
7176

7277
for _, q := range qs {
73-
_, p := q.ans864, q.para864
74-
fmt.Printf("【input】:%v 【output】:%v\n", p, shortestPathAllKeys(p.one))
75-
shortestPathAllKeys1(p.one)
78+
a, p := q.ans864, q.para864
79+
got := shortestPathAllKeys(p.one)
80+
fmt.Printf("【input】:%v 【output】:%v\n", p, got)
81+
if got != a.one {
82+
t.Fatalf("input %v: expected %v, got %v", p.one, a.one, got)
83+
}
84+
// 解法二是指数级 DFS(剪枝不足,作者已注明会超时)。大网格(如第一个 10x10
85+
// 用例)会跑到分钟级导致测试卡住,所以只在小网格上调用它来覆盖代码,
86+
// 大网格只验证解法一。
87+
if len(p.one) <= 5 {
88+
if got1 := shortestPathAllKeys1(p.one); got1 != a.one {
89+
t.Fatalf("input %v: expected %v, shortestPathAllKeys1 got %v", p.one, a.one, got1)
90+
}
91+
}
7692
}
7793
fmt.Printf("\n\n\n")
7894
}

leetcode/1203.Sort-Items-by-Groups-Respecting-Dependencies/1203. Sort Items by Groups Respecting Dependencies_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ type ans1203 struct {
2525
one []int
2626
}
2727

28+
// clone1203 复制一份 group 切片,避免两个解法共享并就地改写同一底层数组。
29+
func clone1203(a []int) []int {
30+
return append([]int(nil), a...)
31+
}
32+
2833
func Test_Problem1203(t *testing.T) {
2934

3035
qs := []question1203{
@@ -38,14 +43,33 @@ func Test_Problem1203(t *testing.T) {
3843
para1203{8, 2, []int{-1, -1, 1, 0, 0, 1, 0, -1}, [][]int{{}, {6}, {5}, {6}, {3}, {}, {4}, {}}},
3944
ans1203{[]int{}},
4045
},
46+
47+
// 祖先项目本身没有分组(group 为 -1),覆盖按 ancestor 自身作组的分支
48+
{
49+
para1203{3, 1, []int{-1, 0, 0}, [][]int{{}, {0}, {}}},
50+
ans1203{[]int{0, 1, 2}},
51+
},
52+
53+
// 组与组之间形成环,无法完成组间拓扑排序,返回空
54+
{
55+
para1203{2, 2, []int{0, 1}, [][]int{{1}, {0}}},
56+
ans1203{[]int{}},
57+
},
4158
}
4259

4360
fmt.Printf("------------------------Leetcode Problem 1203------------------------\n")
4461

4562
for _, q := range qs {
46-
_, p := q.ans1203, q.para1203
47-
fmt.Printf("【input】:%v 【output】:%v\n", p, sortItems1(p.n, p.m, p.group, p.beforeItems))
48-
sortItems(p.n, p.m, p.group, p.beforeItems)
63+
a, p := q.ans1203, q.para1203
64+
// sortItems1 会就地把 group 里的 -1 改写成 m+i,必须给每个解法各传一份
65+
// group 的独立副本,否则第二个解法会读到被污染的 group 而数组越界 panic。
66+
got1 := sortItems1(p.n, p.m, clone1203(p.group), p.beforeItems)
67+
got := sortItems(p.n, p.m, clone1203(p.group), p.beforeItems)
68+
fmt.Printf("【input】:%v 【output】:%v\n", p, got1)
69+
// 拓扑排序的合法顺序不唯一,这里只校验长度:有解时长度为 n,无解时为空。
70+
if len(got) != len(a.one) || len(got1) != len(a.one) {
71+
t.Fatalf("input %v: expected len %v, sortItems got %v, sortItems1 got %v", p, len(a.one), got, got1)
72+
}
4973
}
5074
fmt.Printf("\n\n\n")
5175
}

0 commit comments

Comments
 (0)