Skip to content

Commit 4da0337

Browse files
committed
2026-03-31 00:29
1 parent 3ccc36d commit 4da0337

11 files changed

Lines changed: 231 additions & 1 deletion

File tree

src/p/leetcode/index.rst

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ Merge Sorted Array
907907
:id: merge-sorted-array
908908
:diffculty: Easy
909909
:language: go
910-
:key: 排序
911910
:date: 2026-03-05
912911

913912
很简单的一道题,要不先把 nums1 挪开然后直接合并,要不逆序。之前想复杂了。
@@ -997,3 +996,55 @@ Jump Game II
997996

998997
解法 2
999998
其实和 1 一样,但利用 `farest` 和 `end` 两个变量配合消除了内部循环,保证 `O(n)`
999+
1000+
Minimum Absolute Difference In Bst
1001+
----------------------------------
1002+
1003+
.. leetcode:: _
1004+
:id: minimum-absolute-difference-in-bst
1005+
:diffculty: Easy
1006+
:language: Go
1007+
:key: 二叉搜索树
1008+
:date: 2026-03-30
1009+
1010+
转化为:有序序列里相邻的哪两个数间隔最小。然后中序遍历。
1011+
1012+
Kth Smallest Element In A Bst
1013+
-----------------------------
1014+
1015+
.. leetcode:: _
1016+
:id: kth-smallest-element-in-a-bst
1017+
:diffculty: Medium
1018+
:language:
1019+
:key: 二叉搜索树
1020+
:date: 2026-03-30
1021+
1022+
Validate Binary Search Tree
1023+
---------------------------
1024+
1025+
.. leetcode:: _
1026+
:id: validate-binary-search-tree
1027+
:diffculty: Medium
1028+
:language: go
1029+
:key: 二叉搜索树
1030+
:date: 2026-03-30
1031+
1032+
Search Insert Position
1033+
----------------------
1034+
1035+
.. leetcode:: _
1036+
:id: search-insert-position
1037+
:diffculty: Easy
1038+
:language: go
1039+
:key: 二分查找
1040+
:date: 2026-03-30
1041+
1042+
Search A 2D Matrix
1043+
------------------
1044+
1045+
.. leetcode:: _
1046+
:id: search-a-2d-matrix
1047+
:diffculty: Medium
1048+
:language: go
1049+
:key: 二分查找
1050+
:date: 2026-03-30
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module kth-smallest-element-in-a-bst
2+
3+
go 1.26.1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func kthSmallest(root *TreeNode, k int) int {
12+
cnt := 1
13+
return visit(root, k, &cnt)
14+
}
15+
16+
func visit(n *TreeNode, k int, cnt *int) int {
17+
if n == nil {
18+
return -1
19+
}
20+
if kth := visit(n.Left, k, cnt); kth != -1 {
21+
return kth
22+
}
23+
if *cnt == k {
24+
return n.Val
25+
}
26+
*cnt = *cnt + 1
27+
return visit(n.Right, k, cnt)
28+
}
29+
30+
func main() {
31+
fmt.Println("vim-go")
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module minimum-absolute-difference-in-bst
2+
3+
go 1.26.1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "fmt"
4+
import "math"
5+
6+
type TreeNode struct {
7+
Val int
8+
Left *TreeNode
9+
Right *TreeNode
10+
}
11+
12+
func getMinimumDifference(root *TreeNode) int {
13+
prev := -1
14+
min := math.MaxInt
15+
visit(root, &prev, &min)
16+
return min
17+
}
18+
19+
func visit(n *TreeNode, prev *int, min *int) {
20+
if n == nil {
21+
return
22+
}
23+
visit(n.Left, prev, min)
24+
// fmt.Println("visit:", n.Val, "prev:", *prev)
25+
if *prev != -1 {
26+
v := n.Val - *prev
27+
if *min > v {
28+
*min = v
29+
}
30+
}
31+
*prev = n.Val
32+
visit(n.Right, prev, min)
33+
}
34+
35+
func main() {
36+
fmt.Println("Hello, World!")
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module search-a-2d-matrix
2+
3+
go 1.26.1
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func searchMatrix(matrix [][]int, target int) bool {
6+
return binarySearch(matrix, target, 0, len(matrix) * len(matrix[0]))
7+
}
8+
9+
func access(matrix [][]int, i int) int {
10+
N := len(matrix[0])
11+
m, n := i/N, i%N
12+
return matrix[m][n]
13+
}
14+
15+
func binarySearch(matrix [][]int, target int, left, right int) bool {
16+
if left == right {
17+
return false
18+
}
19+
mid := (left + right) / 2
20+
v := access(matrix, mid)
21+
if v == target {
22+
return true
23+
} else if v > target {
24+
return binarySearch(matrix, target, left, mid)
25+
} else {
26+
return binarySearch(matrix, target, mid+1, right)
27+
}
28+
}
29+
30+
func main() {
31+
fmt.Println(searchMatrix([][]int{{1}}, 2))
32+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module search-insert-position
2+
3+
go 1.26.1
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func searchInsert(nums []int, target int) int {
6+
return binarySearch(nums, target, 0, len(nums))
7+
}
8+
9+
func binarySearch(nums []int, target int, left, right int) int {
10+
// fmt.Println("l r", left, right)
11+
if left == right {
12+
return left
13+
}
14+
mid := (left + right)/2
15+
if nums[mid] == target {
16+
return mid
17+
} else if nums[mid] < target {
18+
return binarySearch(nums, target, mid+1, right)
19+
} else {
20+
return binarySearch(nums, target, left, mid)
21+
}
22+
}
23+
24+
func main() {
25+
fmt.Println(searchInsert([]int{1,3,5,6}, 5))
26+
fmt.Println(searchInsert([]int{1,3,5,6}, 2))
27+
fmt.Println(searchInsert([]int{1,3,5,6}, 7))
28+
fmt.Println(searchInsert([]int{1,2,5,6}, 2))
29+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module validate-binary-search-tree
2+
3+
go 1.26.1

0 commit comments

Comments
 (0)