Merge the chapter of binary tree and searching.

This commit is contained in:
krahets
2023-05-21 19:26:52 +08:00
parent b5eb9ca271
commit d3cc149c5a
45 changed files with 47 additions and 51 deletions

View File

@ -2,7 +2,7 @@
// Created Time: 2022-12-05
// Author: Slone123c (274325721@qq.com)
package chapter_binary_search
package chapter_searching
/* 二分查找(双闭区间) */
func binarySearch(nums []int, target int) int {

View File

@ -2,7 +2,7 @@
// Created Time: 2022-12-05
// Author: Slone123c (274325721@qq.com)
package chapter_binary_search
package chapter_searching
import (
"fmt"
@ -11,14 +11,14 @@ import (
func TestBinarySearch(t *testing.T) {
var (
target = 3
nums = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
target = 6
nums = []int{1, 3, 6, 8, 12, 15, 23, 67, 70, 92}
expected = 2
)
// 在数组中执行二分查找
actual := binarySearch(nums, target)
fmt.Println("目标元素 3 的索引 =", actual)
fmt.Println("目标元素 6 的索引 =", actual)
if actual != expected {
t.Errorf("目标元素 3 的索引 = %d, 应该为 %d", actual, expected)
t.Errorf("目标元素 6 的索引 = %d, 应该为 %d", actual, expected)
}
}