Skip to content

Instantly share code, notes, and snippets.

@balos1
Last active December 17, 2017 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balos1/f1b42da1f09fff22ed5dedcfbda7afca to your computer and use it in GitHub Desktop.
Save balos1/f1b42da1f09fff22ed5dedcfbda7afca to your computer and use it in GitHub Desktop.
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
(num, idx) = (nums[0], 0)
dct = dict([(num, idx)])
for idx2, num2 in enumerate(nums[1:]):
idx2 = idx2+1
if num+num2 == target:
return [idx, idx2]
elif (target-num2) in dct:
return [dct[target-num2], idx2]
else:
dct[num2] = idx2
return []
def main():
nums = [4, 11, 5, 2, 15, 7]
target = 9
print(Solution().twoSum(nums, target))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment