mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-13 12:20:57 +00:00
feat(go): support binary search & fix comments (#691)
This commit is contained in:
@@ -22,3 +22,40 @@ func TestBinarySearch(t *testing.T) {
|
||||
t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinarySearchEdge(t *testing.T) {
|
||||
// 包含重复元素的数组
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
fmt.Println("\n数组 nums = ", nums)
|
||||
|
||||
// 二分查找左边界和右边界
|
||||
for _, target := range []int{6, 7} {
|
||||
index := binarySearchLeftEdge(nums, target)
|
||||
fmt.Println("最左一个元素", target, "的索引为", index)
|
||||
|
||||
index = binarySearchRightEdge(nums, target)
|
||||
fmt.Println("最右一个元素", target, "的索引为", index)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinarySearchInsertion(t *testing.T) {
|
||||
// 无重复元素的数组
|
||||
nums := []int{1, 3, 6, 8, 12, 15, 23, 26, 31, 35}
|
||||
fmt.Println("数组 nums =", nums)
|
||||
|
||||
// 二分查找插入点
|
||||
for _, target := range []int{6, 9} {
|
||||
index := binarySearchInsertionSimple(nums, target)
|
||||
fmt.Println("元素", target, "的插入点的索引为", index)
|
||||
}
|
||||
|
||||
// 包含重复元素的数组
|
||||
nums = []int{1, 3, 6, 6, 6, 6, 6, 10, 12, 15}
|
||||
fmt.Println("\n数组 nums =", nums)
|
||||
|
||||
// 二分查找插入点
|
||||
for _, target := range []int{2, 6, 20} {
|
||||
index := binarySearchInsertion(nums, target)
|
||||
fmt.Println("元素", target, "的插入点的索引为", index)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user