Created
March 1, 2024 05:54
-
-
Save spcbfr/b57ac41ed9d454ad467607c3144a63e6 to your computer and use it in GitHub Desktop.
Two Sum solved in golang with hashmaps
This file contains 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 { | |
m := make(map[int]int) | |
for i := 0; i < len(nums); i++ { | |
curr := nums[i] | |
diff := target - curr | |
if _, ok := m[diff]; ok { | |
return []int{m[diff], i} | |
} | |
m[curr] = i | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment