Skip to content

Instantly share code, notes, and snippets.

@EJSohn
Created April 19, 2019 15:39
Show Gist options
  • Select an option

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

Select an option

Save EJSohn/381c10d58dd394c15f0aaa8eb85da49a to your computer and use it in GitHub Desktop.
translation code snippet (2019.04.20)
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