package main
import (
"fmt"
)
func main() {
nums := []int{1, 5, 7, 8, 9}
target := 10
solutions(nums, target)
}
func solutions(nums []int, target int) (a, b int) {
var dict map[int]int
dict = make(map[int]int)
defer fmt.Println(a, b)
#遍历一遍列表对应的时间复杂度为O(n)
for k, v := range nums {
dict[v] = k
num := target - v
#如果另一个数值不在字典中,则将第一个数值及其的索引报错在字典中 #因为在字典中查找的时间复杂度为O(1),因此总时间复杂度为O(n)
_, ok := dict[v]
if !ok {
dict[v] = k
} else {
fmt.Println(num, v)
return dict[num], k
}
}
return 0, 0
}