- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0001-two-sum.rb
31 lines (25 loc) · 666 Bytes
/
0001-two-sum.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true
# 1. Two Sum
# https://leetcode.com/problems/two-sum
# @param {Integer[]} nums
# @param {Integer} target
# @return {Integer[]}
deftwo_sum(nums,target)
h={}
(0..nums.size - 1).eachdo |i|
complement=target - nums[i]
return[i,h[complement]]ifh.key?(complement)
h[nums[i]]=i
end
end
# **************** #
# TEST #
# **************** #
require"test/unit"
classTest_two_sum < Test::Unit::TestCase
deftest_
assert_equal[0,1].sort,two_sum([2,7,11,15],9).sort
assert_equal[1,2].sort,two_sum([3,2,4],6).sort
assert_equal[0,1].sort,two_sum([3,3],6).sort
end
end