Skip to content

Instantly share code, notes, and snippets.

@kkchu791
Created February 6, 2019 18:08
Show Gist options
  • Save kkchu791/b535d9eea5d3eef84891b445c1874bcc to your computer and use it in GitHub Desktop.
Save kkchu791/b535d9eea5d3eef84891b445c1874bcc to your computer and use it in GitHub Desktop.
# nums = [2, 3, 11, 22], target = 9,
# Because nums[0] + nums[1] = 2 + 7 = 9,
#return [0, 1]
def two_sum(nums, target)
nums.each do |i|
nums.each do |x|
if i + x == target && nums.index(i) != nums.index(x)
return [nums.index(i), nums.index(x)]
end
end
end
end
two_sum([2, 7, 11, 22], 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment