Skip to content

Commit 57a266b

Browse files
authored
fix: fix two sum test cases (#72)
- fix two sum test cases to address issue #68
1 parent 52c972a commit 57a266b

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

leetcode/two_sum/solution.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ class Solution:
44
# Space: O(n)
55
def two_sum(self, nums: list[int], target: int) -> list[int]:
66
seen: dict[int, int] = {}
7+
answers: list[list[int]] = []
8+
79
for i, num in enumerate(nums):
810
complement = target - num
911
if complement in seen:
10-
return [seen[complement], i]
12+
answer = [seen[complement], i]
13+
answers.append(answer)
1114
seen[num] = i
12-
return []
15+
16+
if len(answers) > 1:
17+
raise ValueError(f"Found {len(answers)} answers in the solution: {answers}")
18+
19+
return answers[0] if answers else []

leetcode/two_sum/test_solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def setup_method(self):
2626
([-3, 4, 3, 90], 0, [0, 2]),
2727
([1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 2], 6, [5, 11]),
2828
([2, 1, 9, 4, 4, 56, 90, 3], 8, [3, 4]),
29-
([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 185, [3, 4]),
29+
([89, 90, 91, 92, 93, 97, 98, 99], 185, [3, 4]),
3030
([-1000000000, 1000000000], 0, [0, 1]),
3131
([0, 1], 1, [0, 1]),
3232
([1, 2], 5, []),

leetcode_py/cli/resources/leetcode/json/problems/two_sum.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"([-3, 4, 3, 90], 0, [0, 2])",
7878
"([1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 2], 6, [5, 11])",
7979
"([2, 1, 9, 4, 4, 56, 90, 3], 8, [3, 4])",
80-
"([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 185, [3, 4])",
80+
"([89, 90, 91, 92, 93, 97, 98, 99], 185, [3, 4])",
8181
"([-1000000000, 1000000000], 0, [0, 1])",
8282
"([0, 1], 1, [0, 1])",
8383
"([1, 2], 5, [])",

0 commit comments

Comments
 (0)