-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.go
114 lines (106 loc) · 2.13 KB
/
search.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package gutil
/*
IntBinarySearch 二分查找
input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1
output: 1 or -1 (if key !exists result=-1)
*/
func IntBinarySearch(array []int, key int) int {
low, hight := 0, len(array)-1
for low <= hight {
m := (low + hight) >> 1
if array[m] < key {
low = m + 1
} else if array[m] > key {
hight = m - 1
} else {
// 相等
return m
}
}
return -1
}
/*
Int32BinarySearch 二分查找
input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1
output: 1 or -1 (if key !exists result=-1)
*/
func Int32BinarySearch(array []int32, key int32) int {
low, hight := 0, len(array)-1
for low <= hight {
m := (low + hight) >> 1
if array[m] < key {
low = m + 1
} else if array[m] > key {
hight = m - 1
} else {
// 相等
return m
}
}
return -1
}
/*
Int64BinarySearch 二分查找
input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 1
output: 1 or -1 (if key !exists result=-1)
*/
func Int64BinarySearch(array []int64, key int64) int {
low, hight := 0, len(array)-1
for low <= hight {
m := (low + hight) >> 1
if array[m] < key {
low = m + 1
} else if array[m] > key {
hight = m - 1
} else {
// 相等
return m
}
}
return -1
}
// Int32InSlice 判断int32是否在切片里面
func Int32InSlice(needle int32, haystack []int32) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}
// StringInSlice 判断字符是否在切片里面
func StringInSlice(needle string, haystack []string) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}
// Int64InSlice 判断int64是否在切片里面
func Int64InSlice(needle int64, haystack []int64) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}
// IntInSlice 判断int是否在切片里面
func IntInSlice(needle int, haystack []int) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}
// InterfaceInSlice 判断interface是否在切片里面
func InterfaceInSlice(needle interface{}, haystack []interface{}) bool {
for _, b := range haystack {
if b == needle {
return true
}
}
return false
}