1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// @Title: 两数之和 (Two Sum)
// @Author: 15816537946@163.com
// @Date: 2020-08-04 08:44:03
// @Runtime: 4 ms
// @Memory: 3.7 MB
func twoSum(nums []int, target int) []int {
	/*
	if len(nums) <= 1 {
		return nil
	}

	record := make(map[int]int)
	for i,v := range nums {
		if j, ok := record[target-v]; ok {
			return []int{j,i}
		}
		record[v] = i
	}
	return nil
	*/
	lookup := make(map[int]int)
	for i, v  := range nums {
		if j, ok := lookup[-v]; ok {
			return []int{j,i}
		}
		lookup[v-target] = i
	}
	return []int{}
}