Finish problem 0001-two-sum
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: list[int], target: int) -> list[int]:
|
||||||
|
hashmap = {}
|
||||||
|
for i, n in enumerate(nums):
|
||||||
|
if target - n in hashmap:
|
||||||
|
return [hashmap[target - n], i]
|
||||||
|
hashmap[n] = i
|
||||||
|
return []
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import pytest
|
||||||
|
from solution import Solution
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"nums,target,expected",
|
||||||
|
[
|
||||||
|
([2, 7, 11, 15], 9, [0, 1]),
|
||||||
|
([3, 2, 4], 6, [1, 2]),
|
||||||
|
([3, 3], 6, [0, 1]),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_two_sum(nums, target, expected):
|
||||||
|
assert Solution().twoSum(nums, target) == expected
|
||||||
Reference in New Issue
Block a user