1-Two-Sums

Sat 17 May 2025
# https://leetcode.com/problems/two-sum
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml311; pyv: 3.11.10 (main, Oct  3 2024, 07:29:13) [GCC 11.2.0]'
print(pyu.ps2("haystack-ai ollama-haystack python-dotenv"))
haystack-ai==2.8.0
ollama-haystack is not installed in the current environment.
python-dotenv==0.21.0
from typing import List

class Solution:
  def twoSum(self, nums: List[int], target: int) -> List[int]:
    numToIndex = {}

    for i, num in enumerate(nums):
      if target - num in numToIndex:
        return numToIndex[target - num], i
      numToIndex[num] = i
sol = Solution()
sol.twoSum([2,7,11,15], 9)
(0, 1)
sol.twoSum([3,2,4], 6)
(1, 2)

# Variant 1: Using a single return statement
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        numToIndex = {}
        for i, num in enumerate(nums):
            diff = target - num
            if diff in numToIndex:
                return [numToIndex[diff], i]
            numToIndex[num] = i
Solution().twoSum([2,7,11,15], 9)
[0, 1]


Score: 10

Category: leetcode