-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
35 lines (30 loc) · 1.01 KB
/
main.go
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
30
31
32
33
34
35
package main
import "fmt"
func TwoSum(nums []int, target int) []int {
// here we're going to store the values that we have visited already
// where key is a value that we have visited and index is value in the map
memo := make(map[int]int)
for idx, current := range nums {
// calculate how much we are missing from `current` to `target`
reminder := target - current
// check if in `memo` we see the `reminder`, this means that we've seen
// the number in the past and it's exactly what we need to add to `current`
// to get `target`
seenIdx, found := memo[reminder]
if found {
// we have it in `memo` to return current `idx` and index that we've seen
return []int{seenIdx, idx}
}
// if not found simply remember the current value and an index
memo[current] = idx
}
// should not happen, but still return nil if no solution is found
return nil
}
func main() {
// Example usage
nums := []int{2, 7, 11, 15}
target := 9
result := TwoSum(nums, target)
fmt.Println(result) // Output: [0, 1]
}