Skip to content

Commit 3cc5d7a

Browse files
committed
Ruby Solution for Multiply Strings
1 parent eff3535 commit 3cc5d7a

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ruby/0043-multiply-strings.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def multiply(num1, num2)
2+
3+
return "0" if [num1,num2].include?("0")
4+
5+
res = [0] * (num1.length + num2.length)
6+
7+
num1.reverse!
8+
num2.reverse!
9+
10+
(0..num1.length-1).each do |i|
11+
(0..num2.length-1).each do |j|
12+
digit = num1[i].to_i * num2[j].to_i
13+
res[i+j] += digit
14+
res[i+j+1] += (res[i+j]/10)
15+
res[i+j] = res[i+j] % 10
16+
end
17+
end
18+
19+
res.reverse!
20+
beg = 0
21+
22+
while (beg< res.length && res[beg] ==0)
23+
beg +=1
24+
end
25+
26+
return res.join.to_i.to_s
27+
end

0 commit comments

Comments
 (0)