package main
import (
"fmt"
)
func main() {
nums := []int{1, 3, 4, 6, 7, 10}
target := 17
newArr := twoSum(nums, target)
fmt.Println(newArr)
}
func twoSum(nums []int, target int) [2]int {
maps := make(map[int]int)
for index, value := range nums {
member := target - value
if _, ok := maps[member]; ok {
subscript := [...]int{member, value}
for i,v := range subscript{
for i1,v1 := range nums{
if v == v1 {
subscript[i]= i1
}
}
}
return subscript
} else {
maps[value] = index
}
}
return [2]int{}
}
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)