Closed
Description
Bug Report for https://neetcode.io/problems/two-integer-sum-ii
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
So the problem is asking to return the indices of the values that add up to the target. However, in the example, as listed in the problem shows the input and expected output:
Input: numbers = [1,2,3,4], target = 3
Output: [1,2]
The output should be [0, 1]
assuming that we want the indices as the problem states.
I tried the solution below which works for one of the cases but not for the other one. Apologies if I am overlooking a glaring mistake here.
from typing import List
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
searching = True
pointer_1 = 0
pointer_2 = len(numbers) - 1
while searching:
print("Searching...")
value_1 = numbers[pointer_1]
value_2 = numbers[pointer_2]
if value_1 + value_2 == target:
return [pointer_1, pointer_2]
if value_1 + value_2 > target:
pointer_2 = pointer_2 - 1
elif value_1 + value_2 < target:
pointer_1 = pointer_1 + 1
Also, I noticed that one of the solutions listed in this problem increments the left and right pointer by 1
. Not sure why this is:
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
l, r = 0, len(numbers) - 1
while l < r:
curSum = numbers[l] + numbers[r]
if curSum > target:
r -= 1
elif curSum < target:
l += 1
else:
return [l + 1, r + 1]
return []
Metadata
Metadata
Assignees
Labels
No labels