Skip to content

Instantly share code, notes, and snippets.

@wanderindev
Last active July 1, 2022 20:11
Show Gist options
  • Save wanderindev/decff1bf8a1695529af5b694dd4f7de4 to your computer and use it in GitHub Desktop.
Save wanderindev/decff1bf8a1695529af5b694dd4f7de4 to your computer and use it in GitHub Desktop.
class Solution:
def two_sum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
comp = {}
for index, num in enumerate(nums):
if num in comp:
return [comp[num], index]
comp[target - num] = index
return []
# Test cases
solution = Solution()
# Test cases
nums, target = [2, 7, 11, 15], 9
assert solution.two_sum(nums, target) == [0, 1]
nums, target = [2, 7, 11, 15], 15
assert solution.two_sum(nums, target) == []
nums, target = [-2, -4, 11, 15], 11
assert solution.two_sum(nums, target) == [1, 3]
nums, target = [0, -4, -7, 15], -7
assert solution.two_sum(nums, target) == [0, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment