Skip to content

1. Two Sum

Pattern: HashMap

Problem

Given an array of integers nums and an integer target, return indices of the two numbers that add up to target.

Solution

func twoSum(nums []int, target int) []int {
    tmpTargetToIdx := make(map[int]int)

    for idx1, num := range nums {
        tmpTarget := target - num
        if idx2, ok := tmpTargetToIdx[tmpTarget]; ok {
            return []int{idx1, idx2}
        }
        tmpTargetToIdx[tmpTarget] = idx1
    }

    return []int{}
}

O(n) time, O(n) space.