-
Notifications
You must be signed in to change notification settings - Fork 0
/
704-easy.go
67 lines (60 loc) · 1018 Bytes
/
704-easy.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
60
61
62
63
64
65
66
67
/*
@Time : 2021/9/6 上午10:22
@Author : gaozhichang
@File : binary-search.go
@des: GoLand
*/
package main
import "fmt"
func search(nums []int, target int) int {
n:=len(nums)
if n==0{
return -1
}
hashMap:=make(map[int]int)
for i,v:=range nums{
hashMap[v] = i
}
var findMid func([]int) int
findMid = func(nums []int) int{
fmt.Println(nums)
if len(nums)==0{
return -1
}
if len(nums)==1 && nums[0]!=target{
return -1
}
mid:=len(nums)/2
if nums[mid]==target{
return hashMap[nums[mid]]
}else if nums[mid]>target{
return findMid(nums[:mid])
}else{
return findMid(nums[mid:])
}
}
return findMid(nums)
}
/**
官方
*/
func search2(nums []int, target int) int {
low, high := 0, len(nums)-1
for low <= high {
mid := (high-low)/2 + low
num := nums[mid]
if num == target {
return mid
} else if num > target {
high = mid - 1
} else {
low = mid + 1
}
}
return -1
}
func main() {
nums:=[]int{-1,0,3,5,9,12}
res:=search(nums,2)
fmt.Println(res)
}