Accepted 9 Lines GoLang O(n) Solution


  • 0
    B
    func twoSum(nums []int, target int) []int {
    	helpMap := make(map[int]int)
    	for i, val := range nums {
    		if index, ok := helpMap[target-val]; ok {
    			return []int{index, i}
    		} else {
    			helpMap[val] = i
    		}
    	}
    	return nil
    }
    

Log in to reply
 

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