If we have seen target-nums[j] at index i, then [i, j] is a pair.
Time: , space:
.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen_at = {}
for j, x in enumerate(nums):
if (i := seen_at.get(target-x, -1)) >= 0:
return [i, j]
seen_at[x] = j
return []
Leave a comment