mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
Merge the chapter of binary tree and searching.
This commit is contained in:
@ -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 {
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user