Accepted Go O(n) Solution


  • 0
    W

    func twoSum(nums []int, target int) []int {
    hashMap := make(map[int]int)
    var index []int
    defaultIndex := []int{-1, -1}
    for i, num1 := range nums {
    num2 := target - num1
    if _, ok := hashMap[num2]; ok {
    index = append(index, hashMap[num2], i)
    return index
    }
    hashMap[num1] = i
    }
    return defaultIndex
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.