Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

def two_sum(numbers, target) r = (numbers & numbers.map { |n| target - n }).sort num_1, num_2 = r.first, r.last index_1 = numbers.find_index(num_1) + 1 index_2 = numbers.rindex(num_2) + 1 [index_1, index_2].sort end

why not just use "index_2 = numbers.find_index(num_2) + 1 "?

Because case [1,1,3,4], 2 would fail.

Add I find out that Array#rindex is a better one.

Seems like the old one is a little bit faster than Array#rindex. (About 20ms)

I see, thanks!

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.