DEV Community

schudy
schudy

Posted on

LeetCode 1

I used dictionary to solve this problem.
I create a dictionary named 'seen' to store the nums.
The code will search for the other part of one of num to make the target in 'seen', if there is, then return its index as well as its partners. If there isn't, then store this number to the 'seen' dictionary. And repeat the process till one number find its partner in the dictionary. This way has more space complexity, but low time complexity ,O(n).

Then other way to solve this is traverse each num in nums to search its partner. This use less space while more time, since the time complexity is o(n^2)

Code:
Alt Text

Top comments (0)