diff --git a/leetcode/two_sum/solution.py b/leetcode/two_sum/solution.py index ea4c593..f635406 100644 --- a/leetcode/two_sum/solution.py +++ b/leetcode/two_sum/solution.py @@ -4,9 +4,16 @@ class Solution: # Space: O(n) def two_sum(self, nums: list[int], target: int) -> list[int]: seen: dict[int, int] = {} + answers: list[list[int]] = [] + for i, num in enumerate(nums): complement = target - num if complement in seen: - return [seen[complement], i] + answer = [seen[complement], i] + answers.append(answer) seen[num] = i - return [] + + if len(answers) > 1: + raise ValueError(f"Found {len(answers)} answers in the solution: {answers}") + + return answers[0] if answers else [] diff --git a/leetcode/two_sum/test_solution.py b/leetcode/two_sum/test_solution.py index a251146..de814b7 100644 --- a/leetcode/two_sum/test_solution.py +++ b/leetcode/two_sum/test_solution.py @@ -26,7 +26,7 @@ def setup_method(self): ([-3, 4, 3, 90], 0, [0, 2]), ([1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 2], 6, [5, 11]), ([2, 1, 9, 4, 4, 56, 90, 3], 8, [3, 4]), - ([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 185, [3, 4]), + ([89, 90, 91, 92, 93, 97, 98, 99], 185, [3, 4]), ([-1000000000, 1000000000], 0, [0, 1]), ([0, 1], 1, [0, 1]), ([1, 2], 5, []), diff --git a/leetcode_py/cli/resources/leetcode/json/problems/two_sum.json b/leetcode_py/cli/resources/leetcode/json/problems/two_sum.json index 86875e0..8910120 100644 --- a/leetcode_py/cli/resources/leetcode/json/problems/two_sum.json +++ b/leetcode_py/cli/resources/leetcode/json/problems/two_sum.json @@ -77,7 +77,7 @@ "([-3, 4, 3, 90], 0, [0, 2])", "([1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 2], 6, [5, 11])", "([2, 1, 9, 4, 4, 56, 90, 3], 8, [3, 4])", - "([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99], 185, [3, 4])", + "([89, 90, 91, 92, 93, 97, 98, 99], 185, [3, 4])", "([-1000000000, 1000000000], 0, [0, 1])", "([0, 1], 1, [0, 1])", "([1, 2], 5, [])",