Last active
July 1, 2022 20:11
-
-
Save wanderindev/decff1bf8a1695529af5b694dd4f7de4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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