Accepted Go solution


  • 0
    B

    Since maps in Go return a 0 when a key is not found, I had to store the index values with a +1 and then remove it at the end so as to not get a "false negative".

    func twoSum(nums []int, target int) []int {
        m := make(map[int]int)
        vals := []int{0, 0}
    
        for index,ele := range nums {
    	i := m[ele]
    	if(i == 0){
    		m[target - ele] = index+1
    	} else {
    		vals[0] =i-1
    		vals[1] =index
    		return vals
    	}
        }
        return vals
    }
    

Log in to reply
 

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