I'm doing the famous two sum question from leet code. I noticed, if I wrote:
def findpair(num, target):
for i in range(len(num)):
for j in range(i+1, len(num)):
if num[i] ==num[j]:
continue
if num[i] + num[j] == target:
return(i, j)
mylist = [1, 2, 3 ,4 ,5 , 6]
print(findpair(mylist, 7))
It only returns one pair, which is index 0 and 5.
However, if I change return
to print
, it will give me all the pair. Why is that?