题解 | #两数之和#
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
package main /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ func twoSum(numbers []int, target int) []int { data := make(map[int]int) for i, val := range numbers { idx,has := data[val] if has { if val + val == target { return []int{idx+1,i+1} } } data[val] = i } for i, val := range numbers { idx, has := data[target-val] if has && i != idx{ return []int{i + 1, idx + 1} } } return nil }