A Golang solution use hashmap


  • 0
    A
    func twoSum(nums []int, target int) []int {
    	hash := make(map[int]int)
    	var res []int
    	for i := 0;i < len(nums); i += 1 {
    		toFind := target - nums[i]
    		f, found := hash[toFind]
    		if found == true {
    			res = append(res, f)
    			res = append(res, i)
    			return res
    		}
    		hash[nums[i]] = i
    	}
    	return res
    }
    

Log in to reply
 

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