题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
package main /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ // func twoSum( numbers []int , target int ) []int { // hashTab := map[int]int{} // for i, v := range numbers { // if k, ok := hashTab[target - v]; ok { // return []int{k+1, i+1} // } // hashTab[v] = i // } // return nil // } func twoSum(numbers []int, target int) []int { hashTab := map[int]int{} for i, v := range numbers { if k, ok := hashTab[target-v]; ok { return []int{k+1,i+1} } hashTab[v] = i } return nil }