Skip to content

Instantly share code, notes, and snippets.

@EJSohn
Created April 22, 2019 16:27
Show Gist options
  • Select an option

  • Save EJSohn/e638d7202098d35ed18092712ff4045e to your computer and use it in GitHub Desktop.

Select an option

Save EJSohn/e638d7202098d35ed18092712ff4045e to your computer and use it in GitHub Desktop.
func twoSum(nums []int, target int) []int {
// map from int -> int
m := make(map[int]int)
// python like enumerate
for i, num := range(nums) {
// get value and check existence in map
// and in if statement where ok is the boolean comparison value
if index, ok := m[target - num]; ok {
// construct array and return values
// Go also has multiple returns, but LeetCode requested this format to stay consistent with other languages.
return []int{index, i}
}
// python like set in map
m[num] = i
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment