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]
- """
- # Use a dictionary to store seen numbers and their indices
- nums_dict = {}
- for i, num in enumerate(nums):
- complement = target - num
- if complement in nums_dict:
- return [nums_dict[complement], i]
- nums_dict[num] = i
- # If no solution is found, raise an exception
- raise ValueError("No two sum solution")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement