I used two ways to solve this problem.
Define a dictionary
dic
, traversenumbers
to see if target - number indic
. If it is, then return the two indices array (sorted), else store the number indic
for further matching.
Use two pointers.
l
starts from 0 andr
starts from the last. If target - numbers[l] = numbers [r], then return array [l + 1, r + 1]; else if the sum is bigger than target, mover
left, if is smaller than target, movel
rightward, until they find the target sum.
Top comments (0)