leetcode/0001_two-sum/golang/two-sum-best-approach.go

15 lines
233 B
Go
Raw Normal View History

2022-01-01 16:54:04 +00:00
func twoSum(nums []int, target int) []int {
// Optimal solution: using a hashtable
m := make(map[int]int)
for i, num := range nums {
if j, ok := m[target-num]; ok {
return []int{i, j}
}
m[num] = i
}
return []int{}
}