- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0067-add-binary.rb
62 lines (48 loc) · 1.31 KB
/
0067-add-binary.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
# 67. Add Binary
# https://leetcode.com/problems/add-binary
# @param {String} a
# @param {String} b
# @return {String}
defadd_binary1(a,b)
(a.to_i(2) + b.to_i(2)).to_s(2)
end
# @param {String} a
# @param {String} b
# @return {String}
defadd_binary2(a,b)
a_len= -(a.size)
b_len= -(b.size)
i,carry,res= -1,0,""
whilei >= a_len || i >= b_len
a_bit=i >= a_len ? a[i].to_i : 0
b_bit=i >= b_len ? b[i].to_i : 0
sum=a_bit + b_bit + carry
res=(sum % 2).to_s + res
carry=sum / 2
i -= 1
end
carry > 0 ? "1" + res : res
end
# ********************#
# TEST #
# ********************#
require"test/unit"
classTest_add_binary1 < Test::Unit::TestCase
deftest_
assert_equal"100",add_binary1("11","1")
assert_equal"10101",add_binary1("1010","1011")
assert_equal"100",add_binary2("11","1")
assert_equal"10101",add_binary2("1010","1011")
end
end
require"benchmark"
a="1010111010101010101111100101010100110000101011101101011"
b="100010101000100111011101010101000011100101011110101"
Benchmark.bmdo |x|
x.report{add_binary1(a,b)}
x.report{add_binary2(a,b)}
end
# user system total real
# 0.000013 0.000002 0.000015 ( 0.000009)
# 0.000053 0.000011 0.000064 ( 0.000064)