Approach
My idea was to simply check if the corresponding “missing” number has already been found and if so, return it’s index + current index. Technically it’s faster to check if it exists in nums
instead of in nmap
but I could not find a way to quickly deal with ignoring current index in .index.
Code
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
nmap = {}
for idx, i in enumerate(nums):
missing = target - i
if missing in nmap:
return [idx, nmap[missing]]
nmap[i] = idx
return []