# 1. 题目

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].


# 3. 解答

## 3.1 解法1

func twoSum(nums []int, target int) (output []int) {
size := len(nums)
for i := 0; i < size; i++ {
for j := i + 1; j < size; j++ {
if nums[i] + nums[j] == target {
output = []int{i, j}
return
}
}
}
return
}


## 3.2 解法2

func twoSum(nums []int, target int)[]int {
m := make(map[int]int, len(nums))
for k, v := range nums {
m[v] = k
}

for i, v := range nums {
complement := target - v
if j, ok := m[complement]; ok && j!=i {
return []int{j, i}
}
}

return nil
}


## 3.3 解法3

func twoSum(nums []int, target int)[]int {
m := make(map[int]int, len(nums))
for i, v := range nums {
if j, ok := m[v]; ok {
return []int{j, i}
} else {
m[target-v] = i
}
}

return nil
}