-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (52 loc) · 1.28 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// https://leetcode-cn.com/problems/binary-search/
package main
import (
"fmt"
"sort"
)
func searchImpl1(nums []int, target int) int {
left, right := 0, len(nums)-1
var (
mid int
)
for left <= right {
mid = left + ((right - left) / 2)
value := nums[mid]
if target == value {
return mid
}
if target > value {
left = mid + 1
} else {
right = mid - 1
}
}
return -1
}
// searchImpl2 当元素不存在时,此函数返回待插入的索引
func searchImpl2(nums []int, target int) int {
return sort.SearchInts(nums, target)
}
func searchImpl3(nums []int, target int) int {
for i, v := range nums {
if v == target {
return i
}
}
return -1
}
func search(nums []int, target int) int {
return searchImpl1(nums, target)
// return searchImpl2(nums, target)
// return searchImpl3(nums, target)
}
func main() {
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, -1) == 0)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 0) == 1)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 2) == -1)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 3) == 2)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 5) == 3)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 9) == 4)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 12) == 5)
fmt.Println(search([]int{-1, 0, 3, 5, 9, 12}, 15) == -1)
}