Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def twoSum(self, nums, target):
- """
- :type nums: List[int]
- :type target: int
- :rtype: List[int]
- """
- num_indices = {}
- # Iterate through the array
- for i, num in enumerate(nums):
- # Calculate the complement needed to reach the target
- complement = target - num
- # Check if the complement exists in the dictionary
- if complement in num_indices:
- # Return the indices of the current element and its complement
- return [num_indices[complement], i]
- # If the complement does not exist, store the index of the current element
- num_indices[num] = i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement