Last active
July 21, 2021 20:04
-
-
Save o-az/bdf6a3027d75015fc4d28c8cec5c6c83 to your computer and use it in GitHub Desktop.
Two TwoSum implementations
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
# x + y = target | |
def twoSum(array, targetSum): | |
seen = {} | |
for index, x in enumerate(array): | |
y = targetSum - x | |
if y in seen: | |
return [x, y] | |
else: | |
seen[x] = index | |
return [] | |
# worse | |
def twoNumberSum_2(array, targetSum): | |
for (index_1, item_1) in enumerate(array): | |
for (_, item_2) in enumerate(array[index_1 + 1:]): | |
if item_1 + item_2 == targetSum: | |
return [item_1, item_2] | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment