parent
5be85033f8
commit
271973dd1a
@ -0,0 +1,84 @@
|
||||
package tools
|
||||
|
||||
func BinarySearch(nums []int, target int) int {
|
||||
left := 0
|
||||
right := len(nums) - 1 //注意
|
||||
|
||||
for left <= right { //注意
|
||||
mid := left + (right-left)/2
|
||||
if nums[mid] == target {
|
||||
return mid
|
||||
} else if nums[mid] < target {
|
||||
left = mid + 1 //注意
|
||||
} else if nums[mid] > target {
|
||||
right = mid - 1 //注意
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
func LeftBound(nums []int, target int) int {
|
||||
if len(nums) == 0 {
|
||||
return -1
|
||||
}
|
||||
left := 0
|
||||
right := len(nums) //注意
|
||||
|
||||
for left < right { //注意
|
||||
mid := left + (right-left)/2
|
||||
if nums[mid] == target {
|
||||
right = mid
|
||||
} else if nums[mid] < target {
|
||||
left = mid + 1
|
||||
} else if nums[mid] > target {
|
||||
right = mid //注意
|
||||
}
|
||||
}
|
||||
if left == len(nums) || nums[left] != target {
|
||||
return -1
|
||||
}
|
||||
return left
|
||||
}
|
||||
func LeftBound2(nums []int, target int) int {
|
||||
left := 0
|
||||
right := len(nums) - 1 //注意
|
||||
|
||||
for left <= right { //注意
|
||||
mid := left + (right-left)/2
|
||||
if nums[mid] == target {
|
||||
//收缩右侧边界
|
||||
right = mid - 1
|
||||
} else if nums[mid] < target {
|
||||
//搜索区间变为 [mid+1, right]
|
||||
left = mid + 1 //注意
|
||||
} else if nums[mid] > target {
|
||||
//搜索区间变为 [left, mid-1]
|
||||
right = mid - 1
|
||||
}
|
||||
}
|
||||
if left >= len(nums) || nums[left] != target {
|
||||
return -1
|
||||
}
|
||||
return left
|
||||
}
|
||||
func RightBound(nums []int, target int) int {
|
||||
left := 0
|
||||
right := len(nums) - 1 //注意
|
||||
|
||||
for left <= right { //注意
|
||||
mid := left + (right-left)/2
|
||||
if nums[mid] == target {
|
||||
//收缩左侧边界
|
||||
left = mid + 1
|
||||
} else if nums[mid] < target {
|
||||
//搜索区间变为 [mid+1, right]
|
||||
left = mid + 1 //注意
|
||||
} else if nums[mid] > target {
|
||||
//搜索区间变为 [left, mid-1]
|
||||
right = mid - 1
|
||||
}
|
||||
}
|
||||
if right < 0 || nums[right] != target {
|
||||
return -1
|
||||
}
|
||||
return right
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package tools
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBinarySearch(t *testing.T) {
|
||||
myTest := struct {
|
||||
Arg1 []int
|
||||
Arg2 int
|
||||
Want int
|
||||
}{
|
||||
[]int{1, 4, 7, 9, 10},
|
||||
9,
|
||||
3,
|
||||
}
|
||||
res := BinarySearch(myTest.Arg1, myTest.Arg2)
|
||||
if res != myTest.Want {
|
||||
t.Errorf("BinarySearch(%d,%d) == %d, want %d", myTest.Arg1, myTest.Arg2, res, myTest.Want)
|
||||
}
|
||||
}
|
||||
func TestLeftBound(t *testing.T) {
|
||||
myTest := struct {
|
||||
Arg1 []int
|
||||
Arg2 int
|
||||
Want int
|
||||
}{
|
||||
[]int{1, 4, 4, 4, 7, 9, 10},
|
||||
4,
|
||||
1,
|
||||
}
|
||||
res := LeftBound(myTest.Arg1, myTest.Arg2)
|
||||
if res != myTest.Want {
|
||||
t.Errorf("LeftBound(%d,%d) == %d, want %d", myTest.Arg1, myTest.Arg2, res, myTest.Want)
|
||||
}
|
||||
}
|
||||
func TestRightBound(t *testing.T) {
|
||||
myTest := struct {
|
||||
Arg1 []int
|
||||
Arg2 int
|
||||
Want int
|
||||
}{
|
||||
[]int{1, 4, 4, 4, 7, 9, 10},
|
||||
4,
|
||||
3,
|
||||
}
|
||||
res := RightBound(myTest.Arg1, myTest.Arg2)
|
||||
if res != myTest.Want {
|
||||
t.Errorf("RightBound(%d,%d) == %d, want %d", myTest.Arg1, myTest.Arg2, res, myTest.Want)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue