Problem:
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice.
My code:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for x in nums:
for y in nums:
if nums.index(x) != nums.index(y) and x + y == target :
return [nums.index(x), nums.index(y)]
print (twoSum ([3, 3],6))
Output:
null
In my mind the first "iteration" (I am not quite sure if this is the right term) looks like :
If 0 != 0 (False) and 3 + 3= 6 (True) --> as the first condition is not met, the y loops
So the next iteration looks like:
If 0 != 1 (True) and 3 + 3 = 6 (True)
--> as above conditions are met, the function would return [0,1], but instead the code actually returns null and I do not understand the reason.
So, I would really appreciate, if someone could either explain what's happening, or tell me some keywords, in order to search for the answer on my own :)