Last answered:

08 Jan 2021

Posted on:

05 Jan 2021

0

Two sum solution - why dictionary

Hello,

I would like to ask about the solution to Two sums.
My idea is just to return i,j which goes trough for cycles as indices.

def two_sum(L, target):
    for i in range(len(L)):
        for j in range(i+1, len(L)-1):
            result = L[i] + L[j]
            if result == target:
                return [i, j]
    return -1

What I don't understand is why should we used a dictionary in the solution of this exercise.
Does it bring any benefit?
Thanks,
Best,
Maros J

1 answers ( 0 marked as helpful)
Instructor
Posted on:

08 Jan 2021

0
Hi Maros! Thanks for reaching out. Please accept my apologies for the delayed response. Thank you for letting us know you can proceed with the course!
Good luck and please feel free to post another question should you encounter any difficulties. Thank you.
Best,
Martin
The 365 Team

Submit an answer