Advertisement
Guest User

Two Sum - Gemini

a guest
Feb 16th, 2024
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | Software | 0 0
  1. class Solution(object):
  2.     def twoSum(self, nums, target):
  3.         """
  4.        :type nums: List[int]
  5.        :type target: int
  6.        :rtype: List[int]
  7.        """
  8.         # Use a dictionary to store seen numbers and their indices
  9.         nums_dict = {}
  10.         for i, num in enumerate(nums):
  11.          complement = target - num
  12.          if complement in nums_dict:
  13.             return [nums_dict[complement], i]
  14.          nums_dict[num] = i
  15.  
  16.         # If no solution is found, raise an exception
  17.         raise ValueError("No two sum solution")
  18.  
  19.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement