You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
976 B
61 lines
976 B
package anmo
|
|
|
|
func Search(nums []int32, target int32) int {
|
|
size := len(nums)
|
|
|
|
index := size / 2
|
|
for index >= 0 && index < size {
|
|
num := nums[index]
|
|
if num > target {
|
|
index--
|
|
}
|
|
if num < target {
|
|
index++
|
|
}
|
|
if num == target {
|
|
return index
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func Find(nums1 []int32, nums2 []int32) []int32 {
|
|
data := make(map[int32]int32)
|
|
for _, num := range nums1 {
|
|
data[num] = num
|
|
}
|
|
var ret []int32
|
|
for _, num := range nums2 {
|
|
s, ok := data[num]
|
|
if ok {
|
|
ret = append(ret, s)
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func FindSum(nums []int32, target int32) []int32 {
|
|
|
|
data := make(map[int32]int32)
|
|
for i, num := range nums {
|
|
data[num] = int32(i)
|
|
}
|
|
tr := make(map[int32]int32)
|
|
for i := int32(1); i <= target/2; i++ {
|
|
tr[i] = target - i
|
|
}
|
|
var ret []int32
|
|
for head, tail := range tr {
|
|
index1, ok := data[head]
|
|
if !ok {
|
|
continue
|
|
}
|
|
index2, ok := data[tail]
|
|
if !ok {
|
|
continue
|
|
}
|
|
ret = append(ret, index1, index2)
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|