Skip to content

Instantly share code, notes, and snippets.

@bcowell
Created February 13, 2019 16:38
Show Gist options
  • Save bcowell/dbcdbdbd0d486d361baccf8130a7817c to your computer and use it in GitHub Desktop.
Save bcowell/dbcdbdbd0d486d361baccf8130a7817c to your computer and use it in GitHub Desktop.
two-sum
def twoSum(self, nums: 'List[int]', target: 'int') -> 'List[int]':
index = {}
for i,num in enumerate(nums):
# build dictionary
if index.get(num) == None:
index[num] = i;
# if (target - num) is in dict
if index.get(target - num) != None:
if i != index.get(target-num): # cannot use same element
return [i, index.get(target - num)] # return position of two elements
@bcowell
Copy link
Author

bcowell commented Feb 13, 2019

36 ms solution for leetcode 1. Two Sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment