19 lines
492 B
Python
19 lines
492 B
Python
from typing import List
|
|
|
|
|
|
class Solution:
|
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
|
numMap = {}
|
|
n = len(nums)
|
|
|
|
# Build the hash table
|
|
for i in range(n):
|
|
numMap[nums[i]] = i
|
|
|
|
# Find the complement
|
|
for i in range(n):
|
|
complement = target - nums[i]
|
|
if complement in numMap and numMap[complement] != i:
|
|
return [i, numMap[complement]]
|
|
|
|
return [] # No solution found
|