Skip to content

Instantly share code, notes, and snippets.

@mitchellbusby
Created October 19, 2018 23:28
Show Gist options
  • Save mitchellbusby/02b4fca70e33151312b0a5fae6d8ddfa to your computer and use it in GitHub Desktop.
Save mitchellbusby/02b4fca70e33151312b0a5fae6d8ddfa 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]
"""
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