Created
October 19, 2018 23:28
-
-
Save mitchellbusby/02b4fca70e33151312b0a5fae6d8ddfa to your computer and use it in GitHub Desktop.
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
class Solution: | |
def twoSum(self, nums, target): | |
""" | |
:type nums: List[int] | |
:type target: int | |
:rtype: List[int] | |
""" | |
value_to_index = {} | |
idx = 0 | |
for num in nums: | |
value_to_index[num] = idx | |
idx += 1 | |
for num in nums: | |
try: | |
complement_idx = value_to_index[target - num] | |
current_idx = value_to_index[num] | |
if complement_idx != current_idx: | |
return [current_idx, complement_idx] | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment