I used two ways to solve this problem.
Define a dictionary
dic, traversenumbersto see if target - number indic. If it is, then return the two indices array (sorted), else store the number indicfor further matching.

Use two pointers.
lstarts from 0 andrstarts from the last. If target - numbers[l] = numbers [r], then return array [l + 1, r + 1]; else if the sum is bigger than target, moverleft, if is smaller than target, movelrightward, until they find the target sum.

Top comments (0)