Last active
December 17, 2017 08:10
-
-
Save balos1/f1b42da1f09fff22ed5dedcfbda7afca to your computer and use it in GitHub Desktop.
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
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