Golang O(n) solution


  • 0
    R
    
    func twoSum(nums []int, target int) []int {
    	var r []int
    	m := make(map[int]int)
    	for idx, value := range nums {
    		other := target - value
    		if other_idx, ok := m[other]; ok {
    			r = append(r, other_idx, idx)
    			break
    		}
    		m[value] = idx
    	}
    	return r
    }
    
    

Log in to reply
 

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