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{}
}
©著作权归作者所有:来自51CTO博客作者295631788的原创作品,如需转载,请注明出处,否则将追究法律责任
\
共同学习,写下你的评论
评论加载中...
作者其他优质文章