functwoSum(nums []int, target int) []int { hum_map := make(map[int]int) for i, x := range nums { mus := target - x if _, ok := hum_map[mus]; ok { return []int{i, hum_map[mus]} } hum_map[x] = i } returnnil }
python
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: deftwoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ hum_map = dict() for i, x in enumerate(nums): if target - x in hum_map: return [i, hum_map[target - x]] hum_map[x] = i