Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bug Report for two-integer-sum-ii #3973

Closed
@dcamposliz

Description

@dcamposliz

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.

Image

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp