Created
June 14, 2019 23:21
-
-
Save barce/10887868134ce1b96baa5d7de94db236 to your computer and use it in GitHub Desktop.
leetcode problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Given an array of integers, return indices of the two nums such that they add up to a specific target. | |
# | |
# You may assume that each input would have exactly one solution, and you may not use the same element twice. | |
def two_sum(nums, target) | |
a_copy = [] | |
nums.each {|i| a_copy << i } | |
a_indices = [] | |
a_copy.each_with_index do |val_1,idx_1| | |
nums.each_with_index do |val_2,idx_2| | |
if (((val_1+val_2) == target) && (idx_1!=idx_2)) | |
a_indices << idx_1 | |
a_indices << idx_2 | |
end | |
end | |
end | |
return a_indices.sort!.uniq! | |
end | |
indices = two_sum([1,2,3,5,8,9], 10) | |
puts indices.inspect | |
indices = two_sum([3,3],6) | |
puts indices.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment