Created
April 22, 2019 16:27
-
-
Save EJSohn/e638d7202098d35ed18092712ff4045e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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