Created
February 13, 2019 16:38
-
-
Save bcowell/dbcdbdbd0d486d361baccf8130a7817c to your computer and use it in GitHub Desktop.
two-sum
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
36 ms solution for leetcode 1. Two Sum