- Notifications
You must be signed in to change notification settings - Fork2.4k
Closed
Description
Bug Report forhttps://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 = 3Output: [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.
fromtypingimportListclassSolution:deftwoSum(self,numbers:List[int],target:int)->List[int]:searching=Truepointer_1=0pointer_2=len(numbers)-1whilesearching:print("Searching...")value_1=numbers[pointer_1]value_2=numbers[pointer_2]ifvalue_1+value_2==target:return [pointer_1,pointer_2]ifvalue_1+value_2>target:pointer_2=pointer_2-1elifvalue_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 by1
. Not sure why this is:
classSolution:deftwoSum(self,numbers:List[int],target:int)->List[int]:l,r=0,len(numbers)-1whilel<r:curSum=numbers[l]+numbers[r]ifcurSum>target:r-=1elifcurSum<target:l+=1else:return [l+1,r+1]return []
Metadata
Metadata
Assignees
Labels
No labels