class Solution(object):
def twoSum(self, nums, target):
if len(nums) <= 1:
return False
buff_dict = {}
for i in range(len(nums)):
if nums[i] in buff_dict:
return [buff_dict[nums[i]], i]
else:
buff_dict[target  nums[i]] = i
Here is a Python solution in O(n) time



@Steven_Ge I'm seeing this same issue. Even when I copy the code here verbatim and run it through a debugger, I'm still getting an incorrect offbyone error. Anyone else seeing this or am I completely missing something?

@Steven_Ge You are right, but a minor mistake. The modification should be return [buff_dict[nums[i]]  1, i]

Yes, I think the question was recently made zeroindex based and not starting at 1, like in this solution. Here's the right one:
if len(nums) <= 1: return False mydict = {} for i in range(0, len(nums)): if nums[i] in mydict: return [mydict[nums[i]], i] else: mydict[targetnums[i]] = i

@yylucifer
"return indices of the two numbers such that they add up to a specific target"
means
that it will return a list like [a, b] if exact item found innums
. I guessnums = [11, 2, 2, 7, 11, 15]
(or[]
or[1]
)is not a test case.