「LeetCode」LeetCode
1 1 package main 2 3import "fmt" 4 5func main() { 6 fmt.Println(twoSum([]int{2, 7, 11, 15}, 9)) 7} 8 9func twoSum(nums []int, target int) []int { 10 11 maps := make(map[int]int) 12 13 for i, value := range nums { 14 mid := target - value 15 16 res, ok := maps[mid] 17 if ok { 18 return []int{res, i} 19 } else { 20 maps[value] = i 21 } 22 } 23 return []int{} 24} 120.